-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgradient.nim
51 lines (37 loc) · 1.46 KB
/
gradient.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# From https://ring-lang.github.io/doc1.14/libui.html#draw-gradient
import std/colors
import uing
from uing/rawui import nil
proc rect(ctx: ptr DrawContext, x, y, width, height: float, r, g, b: int, a: float = 1f) =
var
brush: DrawBrush
path = newDrawPath(DrawFillModeWinding)
brush.r = cdouble (r/255)
brush.g = cdouble (g/255)
brush.b = cdouble (b/255)
brush.a = cdouble a
path.addRectangle(x, y, width, height)
`end` path
ctx.fill(path, addr brush)
free path
proc drawHandler(a: ptr AreaHandler; area: ptr rawui.Area; p: ptr AreaDrawParams) {.cdecl.} =
rect(p.context, 0, 0, p.areaWidth, p.areaHeight, 0, 0, 0)
for y in countup(0, 255, 2):
let color = Color(y).extractRGB()
rect(p.context, 0, float y, p.areaWidth, float (1+y), color.r, color.g, color.b)
proc main =
let window = newWindow("Gradient", 500, 500)
let box = newVerticalBox(true)
window.child = box
var handler: AreaHandler
handler.draw = drawHandler
handler.mouseEvent = proc (_: ptr AreaHandler, a: ptr rawui.Area, b: ptr AreaMouseEvent) {.cdecl.} = discard
handler.mouseCrossed = proc (_: ptr AreaHandler, a: ptr rawui.Area, b: cint) {.cdecl.} = discard
handler.dragBroken = proc (_: ptr AreaHandler, a: ptr rawui.Area) {.cdecl.} = discard
handler.keyEvent = proc (_: ptr AreaHandler, a: ptr rawui.Area, b: ptr AreaKeyEvent): cint {.cdecl.} = cint 0
let area = newArea(addr handler)
box.add area, true
show window
mainLoop()
init()
main()