Tip: midInk sub

For color transitions from one rgb to another, you don't need a palette (array) to save all the colors:


' midInk demo.bas SmallBASIC 0.12.8 [B+=MGA] 2017-04-06 from
'midInk test.sdlbas [B+=MGA] 2017-04-06

'how to do a bunch of color transitions WITHOUT a bunch of palettes (arrays)

randomize(timer)
func
rand(n1, n2)
local hi, lo
if n1 > n2 then hi = n1 : lo = n2 else hi = n2 : lo = n1
rand = (rnd * (hi - lo + 1)) \ 1 + lo
end


'ink the color that is percent between the first color and 2nd color
sub midInk(r1, g1, b1, r2, g2, b2, percent)
dr = (r2 - r1) / 100 : dg = (g2 - g1) / 100 : db = (b2 - b1) / 100
color rgb(r1 + dr * percent, g1 + dg * percent, b1 + db * percent)
end

loopcnt = 0
while 1
r1 = rand(0, 255) : g1 = rand(0, 255) : b1 = rand(0, 255)
r2 = rand(0, 255) : g2= rand(0, 255) : b2 = rand(0, 255)
for i = 10 to 1 step -1
midInk r1, g1, b1, r2, g2, b2, i*10
rect xmax/2 - 200 - i*10, ymax/2 - 200 - i*10, xmax/2 + 200 + i*10, ymax/2 + 200 + i*10 filled
next
select case loopcnt mod 3
case 0 : r = 255 : g = 0 : b = 0
case 1 : r = 0 : g = 255 : b = 0
case 2 : r = 0 : g = 0 : b = 255
end select
for i = 200 to 2 step -1
midInk 255, 255, 255, r, g, b, 100 - i/2 'flip percent for outside to inside coloring
circle xmax/2, ymax/2, i filled
next
showpage
delay 300
loopcnt += 1
wend


' midInk demo2.bas SmallBASIC 0.12.8 [B+=MGA] 2017-04-06 from
'midInk test.sdlbas [B+=MGA] 2017-04-06

'how to do a bunch of color transitions WITHOUT a bunch of palettes

randomize(timer)
func
rand(n1, n2)
local hi, lo
if n1 > n2 then hi = n1 : lo = n2 else hi = n2 : lo = n1
rand = (rnd * (hi - lo + 1)) \ 1 + lo
end


'ink the color that is percent between the first color and 2nd color
sub midInk(r1, g1, b1, r2, g2, b2, percent)
dr = (r2 - r1) / 100 : dg = (g2 - g1) / 100 : db = (b2 - b1) / 100
color rgb(r1 + dr * percent, g1 + dg * percent, b1 + db * percent)
end

'do blue sky and grass with more control on first and last color
horizon = 2*ymax/3
for
i = 0 to horizon
midInk 0, 0, 168, 63, 128, 255, i/horizon * 100
line 0, i, xmax, i
next
for i = horizon to ymax
midInk 0, 164, 0, 20, 96, 40, 100 * (i - horizon) / (ymax - horizon)
line 0, i, xmax, i
next
pause

Thanks MGA!

Runs very impressively on my 'duty speed-test' (dog) box. Runs quick here, will be a whirl on newer machines. But! Increasingly, we are seeing reduced-resources & capacity machines offered in consumer outlets ... obviously the popular item. Raspberry Pi!

My immediate wonder, was the prospect of using MidInk for anti-alias lines, and especially Bresenham thick-line rendering. This also has promise for resolving end-point & intersection considerations. I would not have thought it could be this fast. Very nice!

Here is how the orthodox Bresenham routine performs the thick-line transition-shading goal:

' setPixelColor(x0, y2 += sy, max(0,255*(abs(e2)/ed-wd+1))); ' C src

MAX(0, 255 * (abs(e2) / ed - wd + 1))

... with e2 being the working-var for deviation from the line-vector, ed being the (constant) non-linear transition-slope (sum-of-squares) across the width of the thick-line, and wd being the adjusted working-width var.

I was wondering whether a Palette-array would be the way to go ... better to avoid it, or will that have a role, too?

This is very good & useful code here!

Got to crank for work. Remarks on Petal Eq., tonight.

Ted

I would think with shading you have to worry about a light source, not to mention a bunch of other 3d stuff.