September 23, 2019, 03:13:15 PM

Author Topic: HSl Color picker help  (Read 46 times)

Offline blinkok

  • Full Member
  • ***
  • Posts: 121
HSl Color picker help
« on: September 10, 2019, 02:40:33 AM »
I am using some code i found on the TGC board which was for DarkBasic
I have attempted to convert it to AGK and am having a slight issue.
When i run my code i get the following color range;


If i have a look at another apps colour range it looks like this;


As you can see my color range is getting that yellow band over on the right whereas the other one ends with that red colour.

Here is the code;
Code: [Select]


// Project: color_picker2
// by blendman and another guy (I don't remember its name :( )
// Created: 2017-02-20


// set window properties
SetWindowTitle( "color_picker2" )
SetWindowSize( 1024, 768, 0 )

// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

SetClearColor(0x80, 0x80, 0x80)

ColorPicker(100, 100)

do

print("("+str(GetPointerX())+","+str(GetPointerY())+")")
sync()
   
loop
end




type ColorPickerStruct
PickerWindowId as integer
ArrowId as integer
BrightnessId as integer
Shadow as integer
Light as integer
Init as integer
endtype

global ColorPickerData as ColorPickerStruct

function ColorPicker(x as float, y as float)

if ColorPickerData.PickerWindowId = 0
ColorPickerData.PickerWindowId = ColorPickerInit(x, y)
endif

endfunction

function ColorPickerShadowLight(s as integer, l as integer)
ColorPickerData.Init = 1
ColorPickerData.Shadow = s
ColorPickerData.Light = l
endfunction

function ColorPickerInit(x as float, y as float)
hue as float
sat as float
lum as float
c as integer
id as integer

if ColorPickerData.Init = 0
ColorPickerData.Shadow = 0x20
ColorPickerData.Light = 0xc0
ColorPickerData.Init = 1
endif

/*
if ColorPickerData.ArrowId = 0
ClearScreen()
DrawLine( 0,  3, 7,  0, 0, 0, 0)
DrawLine(7,  0, 7, 7, 0, 0, 0)
DrawLine(7, 7,  0,  3, 0, 0, 0)
ColorPickerData.ArrowId = CreateSprite(GetImage(0, 0, 8, 8))
SetSpritePosition(ColorPickerData.ArrowId , 300, 300)
endif
*/

ClearScreen()

lum = 1

for hue=0 to 360.0 step 6.0
for sat=100 to 0 step -1
c = HSL2RGB(hue, (sat * 0.01), ( sat * 0.01))
x = (hue / 6) + 1
y = (100 - sat) + 1
drawbox(x, y, x+1, y+1, c, c, c, c, 1)
next
next

drawline( 0, 101, 61, 101, ColorPickerData.Light,  ColorPickerData.Light,  ColorPickerData.Light)
drawline( 0,   0,  0, 101, ColorPickerData.Shadow, ColorPickerData.Shadow, ColorPickerData.Shadow)
drawline(61,   0, 61, 101, ColorPickerData.Light,  ColorPickerData.Light,  ColorPickerData.Light)

id = CreateSprite(GetImage(0, 0, 62, 102))

SetSpritePosition(id, x, y)

endfunction id

function HSL2RGB(H as float, S as float, L as float)
   `Sanitize the input...
   
  if H < 0.0
    H = 0.0
    elseif H > 360.0
    H = 360.0
    endif
   
   if S < 0.0
    S = 0.0
   elseif S > 1.0
    S =   1.0
   endif
   
   if L < 0.0
    L = 0.0
   elseif L > 1.0
    L = 1.0
   endif

   `Section of the Hue
   Hi as integer : Hi = mod((H / 60.0), 6)

   `f is a fraction of the section
   f as float : f = (H / 60.0) - Hi

   `p, q and t are temp variables used to work out the rgb values before we know where to put them
   p as float : p = L * (1.0 - S)
   q as float : q = L * (1.0 - (f * S))
   t as float : t = L * (1.0 - ((1.0 - f) * S))

   `Depending on the section worked out above, we store the rgb value appropriately...
   R as float : G as float : B as float
   select Hi
      case 0 : R = L : G = t : B = p : endcase
      case 1 : R = q : G = L : B = p : endcase
      case 2 : R = p : G = L : B = t : endcase
      case 3 : R = p : G = q : B = L : endcase
      case 4 : R = t : G = p : B = L : endcase
      case 5 : R = L : G = p : B = q : endcase
   endselect

   `Sanitize the output - just incase of floating point errors...
   if R < 0.0
    R = 0.0
   elseif R > 1.0
    R = 1.0
   endif
   
   if G < 0.0
    G = 0.0
   elseif G > 1.0
    G = 1.0
   endif
   
   if B < 0.0
    B = 0.0
   elseif B > 1.0
    B = 1.0
   endif

   `Convert R, G, B to a colour

color as integer : color = MakeColor(R * 255, G * 255, B * 255)

endfunction color

Can anyone tell me what i'm doing wrong here?
Any help is greatly appreciated
Thanks