-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gradients.fluid
196 lines (177 loc) · 7.34 KB
/
gradients.fluid
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
--[[
Gradient Demonstration
This is a replica of an AGG demo that was originally built to test focal gradient capabilities.
--]]
require 'gui/window'
require 'gui/combobox'
local win, viewport, gradient, gradientVP
local adjust = 'focal', focal_ellipse
local colours = {
greyscale = { { offset=0, r=0, g=0, b=0 }, { offset=1.0, r=255, g=255, b=255 } },
magma = { { offset=0, r=0.37281, g=0.11883, b=3.53583 }, { offset=0.25, r=79.14741, g=17.77401, b=123.21243 }, { offset=0.5, r=179.40066, g=53.71269, b=122.157495 }, { offset=0.75, r=251.07861, g=132.781815, b=96.05799 }, { offset=1, r=251.698515, g=252.81669, b=191.12352 } },
mako = { { offset=0, r=11.48503425, g=3.7799772, b=5.33517885 }, { offset=0.25, r=61.9298457, g=51.8813871, b=105.276342 }, { offset=0.5, r=52.9219758, g=120.67703385, b=162.20886855 }, { offset=0.75, r=73.261908, g=193.08043335, b=173.1428682 }, { offset=1, r=222.40837095, g=244.6998054, b=228.7997292 } }
}
----------------------------------------------------------------------------------------------------------------------
function buildStops(Theme)
local stops = { }
for _, col in pairs(colours[Theme]) do
local vc = struct.new('GradientStop')
vc.offset = col.offset
vc.rgb.red = col.r / 255
vc.rgb.green = col.g / 255
vc.rgb.blue = col.b / 255
vc.rgb.alpha = 1.0
table.insert(stops, vc)
end
return stops
end
function buildScene()
gradient = viewport.scene.new('VectorGradient', {
name = 'gradient', type = 'radial', stops = buildStops('magma'),
cX = '50%', cY = '50%',
fX = '50%', fY = '50%', focalRadius = 100,
units = 'userSpace', radius = 100,
spreadMethod = 'reflect'
})
viewport.scene.mtAddDef('gradient', gradient)
gradientVP = viewport.new('VectorViewport', { x = 0, y = 20, width = '100%', height='100%' })
local rect = gradientVP.new('VectorRectangle', { x = 0, y = 0, width='100%', height='100%', fill='url(#gradient)' })
focal_ellipse = gradientVP.new('VectorEllipse', {
cx ='50%', cy='50%', radius=100, stroke='rgb(255,255,255)', strokeWidth=1
})
local cmbGradient = gui.combobox({
target = viewport,
label = 'Gradient:',
text = 'Radial',
x = win.margins.left,
y = 10,
items = {
{ item='Radial' },
{ item='Conic' },
{ item='Diamond' },
{ item='Contour' },
{ item='Linear' }
},
events = {
activate = function(Widget, Text)
if Text == 'Radial' then
gradient.type = 'radial'
focal_ellipse.visibility = 'visible'
adjust = 'focal'
gradient.cX = '50%'
gradient.cY = '50%'
elseif Text == 'Conic' then
focal_ellipse.visibility = 'hidden'
gradient.type = 'conic'
adjust = 'center'
elseif Text == 'Diamond' then
focal_ellipse.visibility = 'hidden'
gradient.type = 'diamond'
adjust = 'center'
elseif Text == 'Contour' then
focal_ellipse.visibility = 'hidden'
gradient.type = 'contour'
adjust = 'center'
elseif Text == 'Linear' then
focal_ellipse.visibility = 'hidden'
gradient.type = 'linear'
adjust = 'center'
gradient.x1 = '50%'
gradient.y1 = '50%'
gradient.x2 = '100%'
gradient.y2 = '100%'
end
gradientVP.scene.surface.mtScheduleRedraw()
end
}
})
local cmbSpread = gui.combobox({
target = viewport,
label = 'Spread Method:',
text = 'Reflect',
x = cmbGradient.viewport.x + cmbGradient.viewport.width + 10,
y = 10,
items = { { item='Pad' }, { item='Repeat' }, { item='Reflect' }, { item='Clip' } },
events = {
activate = function(Widget, Text)
if Text == 'Pad' then
gradient.spreadMethod = VSPREAD_PAD
elseif Text == 'Reflect' then
gradient.spreadMethod = VSPREAD_REFLECT
elseif Text == 'Repeat' then
gradient.spreadMethod = VSPREAD_REPEAT
elseif Text == 'Clip' then
gradient.spreadMethod = VSPREAD_CLIP
end
gradientVP.scene.surface.mtScheduleRedraw()
end
}
})
local cmbColours = gui.combobox({
target = viewport,
label = 'Colour Map:',
text = 'Magma',
x = cmbSpread.viewport.x + cmbSpread.viewport.width + 10,
y = 10,
items = { { item='Magma' }, { item='Mako' }, { item='Greyscale' } },
events = {
activate = function(Widget, Text)
local stops = buildStops(Text:lower())
if nz(stops) then
gradient.stops = stops
gradientVP.scene.surface.mtScheduleRedraw()
end
end
}
})
gradientVP.y = cmbGradient.viewport.y + cmbGradient.viewport.height + 12
end
----------------------------------------------------------------------------------------------------------------------
win = gui.window({ title='Gradient Demo', insideWidth=1024, insideHeight=800, center=true })
viewport = win:clientViewport({
aspectRatio = ARF_MEET
})
buildScene()
local button_held = false
check(gradientVP.mtSubscribeInput(bit.bor(JTYPE_MOVEMENT, JTYPE_BUTTON, JTYPE_REPEATED), function(Viewport, Events)
local ev = Events
while ev do
if (ev.type == JET_ABS_XY) then
if button_held == 1 then
if adjust == 'focal' then
gradient.fX = (ev.x / gradientVP.width * 100) .. '%'
gradient.fY = (ev.y / gradientVP.height * 100) .. '%'
elseif adjust == 'radius' then
a = math.abs(ev.x - (gradientVP.width * 0.5))
b = math.abs(ev.y - (gradientVP.height * 0.5))
gradient.radius = math.sqrt((a * a) + (b * b))
else
gradient.cX = (ev.x / gradientVP.width * 100) .. '%'
gradient.cY = (ev.y / gradientVP.height * 100) .. '%'
gradient.x2 = (ev.x / gradientVP.width * 100) .. '%'
gradient.y2 = (ev.y / gradientVP.height * 100) .. '%'
end
gradientVP.scene.surface.mtScheduleRedraw()
end
elseif (ev.type == JET_BUTTON_1) then -- LMB
button_held = ev.value
if adjust == 'focal' then
gradient.fX = (ev.x / gradientVP.width * 100) .. '%'
gradient.fY = (ev.y / gradientVP.height * 100) .. '%'
elseif adjust == 'radius' then
a = math.abs(ev.x - (gradientVP.width * 0.5))
b = math.abs(ev.y - (gradientVP.height * 0.5))
gradient.radius = math.sqrt((a * a) + (b * b))
else
gradient.cX = (ev.x / gradientVP.width * 100) .. '%'
gradient.cY = (ev.y / gradientVP.height * 100) .. '%'
gradient.x2 = (ev.x / gradientVP.width * 100) .. '%'
gradient.y2 = (ev.y / gradientVP.height * 100) .. '%'
end
gradientVP.scene.surface.mtScheduleRedraw()
end
ev = ev.next
end
end))
win:show()
processing.sleep()