[edit: Removed the older versions of the function]
This is as streamlined as I can get it: (I changed the intensity value to an integer from 0 to 255)
set display mode 1024,768,32
hide mouse
red = 255*65536
grn = 255*256
blu = 255
dim color(6,1)
color(0) = red+blu
color(2) = red+grn
color(4) = blu+grn
color(6) = red+blu
set text opaque
sync on
w = screen width()/6 :`color bar width
ival = 255
jval = 255
do
rem adjust colour ratios
mc = keystate(16) + keystate(30)*2
nc = keystate(17) + keystate(31)*2
if mc = 1 and ival < 255 then inc ival, 1
if mc = 2 and ival > 0 then dec ival, 1
if nc = 1 and jval < 255 then inc jval, 1
if nc = 2 and jval > 0 then dec jval, 1
rem mix in-between colours
for i = 1 to 5 step 2
rem mix pigments
color(i) = mixPigment(color(i-1),color(i+1),ival,jval)
rem mix lights
color(i,1) = mixLight(color(i-1),color(i+1),ival,jval)
next i
rem draw colour bars
for x = 0 to 5
ink color(x),0
box x*w,0,(x+1)*w,screen height()-1
if x&1
ink color(x,1),0
box x*w,screen height()/2,(x+1)*w,screen height()-1
endif
next x
rem user info
ink 0,-2
text 0,0,"The top half of the screen mixes pigments, the bottom half mixes lights."
text 0,16,"Use Q and A to adjust the intensity of the left-hand colour."
text 0,32,"Use W and S to adjust the intensity of the right-hand colour."
text 0,48,"L: "+str$(ival)+", R: "+str$(jval)
sync
loop
end
function mixPigment(i,j,ival,jval)
`invert colours and apply intensity (65793 = 1 + 256 + 65536)
i = (16777215 - i) & 65793 * ival
j = (16777215 - j) & 65793 * jval
`combine colours and reinvert
k = 16777215 - (i|j)
endfunction k
function mixLight(i,j,ival,jval)
`apply intensity (65793 = 1 + 256 + 65536)
i = i & 65793 * ival
j = j & 65793 * jval
`combine colours
k = i|j
endfunction i
I really don't understand why the mixLight function is messing up, it looks like it should work perfectly.
You may have noticed that I stumbled across a simple way to limit the brightness of a colour:
color = 65535
hide mouse
sync rate 0
sync on
wait 2000
repeat
for bri = 0 to 255
cls colorBri(color,bri)
sync
next bri
until scancode()
function colorBri(col,bri)
ret = col & 65793 * bri
endfunction ret
[edit]
Bugger, mixPigment still not working quite as it should. I tested Ped + Yellow and it gives Red.
[edit]
FINALLY! Wow that was surprisingly challenging. This can probably be simplified but it works just like paints:
set display mode 1024,768,32
hide mouse
red = 255*65536
grn = 255*256
blu = 255
dim color(12,1)
color(0) = red+blu
color(2) = red+grn
color(4) = blu+grn
color(6) = red+blu
color(8) = red
color(10) = grn
color(12) = blu
set text opaque
sync on
w = screen width()/13 :`color bar width
ival = 0
jval = 0
do
rem adjust colour ratios
oldmc = mc
oldnc = nc
mc = keystate(16) + keystate(30)*2
nc = keystate(17) + keystate(31)*2
if mc > oldmc
if mc = 1 and ival < 255 then inc ival, 1
if mc = 2 and ival > 0 then dec ival, 1
endif
if nc > oldnc
if nc = 1 and jval < 255 then inc jval, 1
if nc = 2 and jval > 0 then dec jval, 1
endif
rem mix in-between colours
for i = 1 to 11 step 2
rem mix pigments
color(i) = mixPigment(color(i-1),color(i+1),ival,jval)
rem mix lights
` color(i,1) = mixLight(color(i-1),color(i+1),ival,jval)
next i
rem draw colour bars
for x = 0 to 12
ink color(x),0
box x*w,0,(x+1)*w,screen height()-1
` if x&1
` ink color(x,1),0
` box x*w,screen height()/2,(x+1)*w,screen height()-1
` endif
next x
rem user info
ink 0,-2
` text 0,0,"The top half of the screen mixes pigments, the bottom half mixes lights."
text 0,16,"Use Q and A to adjust the amount of the left-hand colour."
text 0,32,"Use W and S to adjust the amount of the right-hand colour."
text 0,48,"L: "+str$(ival)+", R: "+str$(jval)
sync
loop
end
function mixPigment(i,j,ival,jval)
i = 16777215 - i
j = 16777215 - j
ir = rgbr(i)*ival
ig = rgbg(i)*ival
ib = rgbb(i)*ival
jr = rgbr(j)*jval
jg = rgbg(j)*jval
jb = rgbb(j)*jval
kr = ir+jr
kg = ig+jg
kb = ib+jb
x = kr
if kg > x then x = kg
if kb > x then x = kb
if x > 255
y = x/255
kr = kr/y
kg = kg/y
kb = kb/y
endif
k = 16777215 - rgb(kr,kg,kb)
endfunction k
function mixLight(i,j,ival,jval)
`apply intensity (65793 = 1 + 256 + 65536)
i = i & 65793 * ival
j = j & 65793 * jval
`combine colours
k = i|j
endfunction i
The difficulty in learning is not acquiring new knowledge but relinquishing the old.