I haven't debugged my HSL routines yet, but I have got a set of working HSV routines - I've plugged them into your code, and it works just fine.
Your code modified:
fade_init()
sync on:sync rate 0
color backdrop rgb(0,0,0)
load image "1.jpg", 1, 1
make object plain 1,300,300
texture object 1,1
do
`fade out image 0,works on almost ALL gfx cards !!!
`you need to input img width n height
fade_out(1,300,300)
text 10,10,"fps: "+str$(screen fps())
sync
loop
`make an array and a type needed
`to use the fading effect
function fade_init()
gosub fadeinit
endfunction
fadeinit:
`glbals for HSL/RGB conversion
type pixel
R as float
G as float
B as float
H as float
S as float
L as float
endtype
dim pixel(0) as pixel
return
`uses memblocks of no. 100 and over
function fade_out(imgno,width,height)
if memblock exist(100+imgno) = 0 then make memblock from image 100+imgno,imgno
memno = 100+imgno
for pixel = 1 to (width*height)
location = getLocation(pixel, 1)* 4 + 12
old = memblock dword(memno, location)
` if old && 0xffffff
HSV = RGBtoHSV( old )
H# = HSV_Hue( HSV )
S# = HSV_Saturation( HSV )
V# = HSV_Value( HSV )
HSV = HSV_Build( H#, S#, V# * 0.98 )
new = HSVtoRGB( HSV )
write memblock dword memno, location, new
` endif
next pixel
make image from memblock imgno,memno
endfunction
function getLocation(x as integer, y as integer)
offset = (y-1)*width + x - 1
endfunction offset
My colour.dba file (just include in your project)
function RGBtoHSV(Colour as dword)
local Red as float
local Green as float
local Blue as float
local MaxColour as float
local MinColour as float
local Hue as float
local Saturation as float
local Value as float
Red = rgbr(Colour) / 255.0
Green = rgbg(Colour) / 255.0
Blue = rgbb(Colour) / 255.0
MaxColour = max3(Red, Green, Blue)
MinColour = min3(Red, Green, Blue)
Value = MaxColour
if (MaxColour = 0.0)
Saturation = 0.0
else
Saturation = (MaxColour - MinColour) / MaxColour
endif
if Saturation = 0
Hue = 0
else
if Red = MaxColour
Hue = (Green - Blue) / (MaxColour - MinColour)
else
if Green = MaxColour
Hue = 2.0 + (Blue - Red) / (MaxColour - MinColour)
else
Hue = 4.0 + (Red - Green) / (MaxColour - MinColour)
endif
endif
Hue = wrapvalue( Hue * 60.0 )
endif
Result = HSV_Build(Hue, Saturation, Value)
endfunction Result
function HSVtoRGB(HSV as dword)
local H as float
local S as float
local V as float
local I as integer
local F as float
local P as float
local Q as float
local T as float
H = HSV_Hue(HSV)
S = HSV_Saturation(HSV)
V = HSV_Value(HSV)
if S = 0.0 then exitfunction rgb(V*255, V*255, V*255)
H = H / 60.0
I = int(H)
F = H - I
P = V * (1.0 - S)
Q = V * (1.0 - S * F)
T = V * (1.0 - S * (1.0 - F))
select I
case 0 : exitfunction rgb(V*255, T*255, P*255) : endcase
case 1 : exitfunction rgb(Q*255, V*255, P*255) : endcase
case 2 : exitfunction rgb(P*255, V*255, T*255) : endcase
case 3 : exitfunction rgb(P*255, Q*255, V*255) : endcase
case 4 : exitfunction rgb(T*255, P*255, V*255) : endcase
case 5 : exitfunction rgb(V*255, P*255, Q*255) : endcase
endselect
endfunction 0
function HSV_Hue(HSV as dword)
local Hue as float
Hue = (HSV >> 16) / 90.0
endfunction Hue
function HSV_Saturation(HSV as dword)
local Saturation as float
Saturation = ( (HSV >> 8) && 0xff ) / 255.0
endfunction Saturation
function HSV_Value(HSV as dword)
local Value as float
Value = (HSV && 0xff) / 255.0
endfunction Value
function HSV_Build(Hue as float, Saturation as float, Value as float)
local HSV as dword
Hue = wrapvalue( Hue ) * 90.0
Saturation = int(Saturation * 255.0 ) && 0xff
Value = int( Value * 255.0 ) && 0xff
HSV = ( int(Hue) << 16 ) || ( int(Saturation) << 8 ) || int( Value )
endfunction HSV
function Max3(a as float, b as float, c as float)
if a < b then a = b
if a < c then a = c
endfunction a
function Min3(a as float, b as float, c as float)
if a > b then a = b
if a > c then a = c
endfunction a
EDIT - BTW, my routines work with a Hue ranging from 0 to 360, ie degrees around the colour circle, and the Saturation and Value ranging from 0 to 1. The HSV_Build function encodes these into a single dword, making it easy to pass colour values around.