Code: Select all
Enumeration
#RedRGBText
#RedRGBSpin
#RedRGBCanvas
#GreenRGBText
#GreenRGBSpin
#GreenRGBCanvas
#BlueRGBText
#BlueRGBSpin
#BlueRGBCanvas
#LinkCheck
#HueText
#HueSpin
#HueCanvas
#SatText
#SatSpin
#SatCanvas
#LightText
#LightSpin
#LightCanvas
#HTMLText
#HTMLString
EndEnumeration
Structure HSLStructure
Hue.i
Saturation.i
Light.i
EndStructure
Procedure.f Max3F(a.f, b.f, c.f)
If a > b
If a > c
ProcedureReturn a
Else
ProcedureReturn c
EndIf
Else
If b > c
ProcedureReturn b
Else
ProcedureReturn c
EndIf
EndIf
EndProcedure
Procedure.f Min3F(a.f, b.f, c.f)
If a < b
If a < c
ProcedureReturn a
Else
ProcedureReturn c
EndIf
Else
If b < c
ProcedureReturn b
Else
ProcedureReturn c
EndIf
EndIf
EndProcedure
Procedure RGB2HSL(RedRGB, GreenRGB, BlueRGB, *HSL.HSLStructure) ; converts RGB-color *c to HSL and returns *c. No check if RGB is made!
Protected r.f, g.f, b.f, max.f, min.f, delta.f, hue.f, saturation.f, light.f
r = RedRGB / 255
g = GreenRGB / 255
b = BlueRGB / 255
max = Max3F(r,g,b)
min = Min3F(r,g,b)
delta = max - min
; get lightness
light = (max + min) / 2.0
If delta <> 0.0
; get saturation
If light <= 0.5
saturation = delta/(max+min)
Else
saturation = delta/(2-max-min)
EndIf
; get hue
If r = max
hue = (g-b)/delta
If g < b
hue + 6.0
EndIf
ElseIf g = max
hue = (b-r)/delta + 2.0
ElseIf b = max
hue = (r-g)/delta + 4.0
EndIf
*HSL\Hue = hue * 60.0
If *HSL\Hue < 0.0
*HSL\Hue + 360.0
EndIf
Else
; all colors have the same value
*HSL\Saturation = 0
*HSL\Hue = 0 ; *c\h is even undefined
EndIf
*HSL\Saturation = saturation * 100
*HSL\Light = light * 100
EndProcedure
Procedure CalcColors()
Protected.i RedRGB, GreenRGB, BlueRGB
Protected HSL.HSLStructure
RedRGB = GetGadgetState(#RedRGBSpin)
SetGadgetColor(#RedRGBCanvas, #PB_Gadget_BackColor, RGB(RedRGB, 0, 0))
GreenRGB = GetGadgetState(#GreenRGBSpin)
SetGadgetColor(#GreenRGBCanvas, #PB_Gadget_BackColor, RGB(0, GreenRGB, 0))
BlueRGB = GetGadgetState(#BlueRGBSpin)
SetGadgetColor(#BlueRGBCanvas, #PB_Gadget_BackColor, RGB(0, 0, BlueRGB))
RGB2HSL(RedRGB, GreenRGB, BlueRGB, @HSL)
SetGadgetState(#HueSpin, HSL\Hue)
SetGadgetState(#SatSpin, HSL\Saturation)
SetGadgetState(#LightSpin, HSL\Light)
Debug HSL\Hue
Debug HSL\Saturation
Debug HSL\Light
SetGadgetText(#HTMLString, "#" + RSet(Hex(RedRGB), 2, "0") + RSet(Hex(GreenRGB), 2, "0") + RSet(Hex(BlueRGB), 2, "0"))
EndProcedure
OpenWindow(0, 0, 0, 300, 200, "Current color", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
TextGadget(#RedRGBText, 10, 10, 40, 20, "Red:", #PB_Text_Right)
SpinGadget(#RedRGBSpin, 60, 10, 50, 20, 0, 255, #PB_Spin_Numeric)
SetGadgetState(#RedRGBSpin, 0)
TextGadget(#RedRGBCanvas, 60, 30, 50, 4, "")
DisableGadget(#RedRGBCanvas, #True)
TextGadget(#GreenRGBText, 10, 50, 40, 20, "Green:", #PB_Text_Right)
SpinGadget(#GreenRGBSpin, 60, 50, 50, 20, 0, 255, #PB_Spin_Numeric)
SetGadgetState(#GreenRGBSpin, 0)
TextGadget(#GreenRGBCanvas, 60, 70, 50, 4, "")
DisableGadget(#GreenRGBCanvas, #True)
TextGadget(#BlueRGBText, 10, 90, 40, 20, "Blue:", #PB_Text_Right)
SpinGadget(#BlueRGBSpin, 60, 90, 50, 20, 0, 255, #PB_Spin_Numeric)
SetGadgetState(#BlueRGBSpin, 0)
TextGadget(#BlueRGBCanvas, 60, 110, 50, 4, "")
DisableGadget(#BlueRGBCanvas, #True)
CheckBoxGadget(#LinkCheck, 10, 130, 70, 20, "Link colors")
TextGadget(#HueText, 130, 10, 40, 20, "Hue:", #PB_Text_Right)
SpinGadget(#HueSpin, 180, 10, 50, 20, 0, 360, #PB_Spin_Numeric)
SetGadgetState(#HueSpin, 0)
TextGadget(#HueCanvas, 180, 30, 50, 4, "")
DisableGadget(#HueCanvas, #True)
TextGadget(#SatText, 130, 50, 40, 20, "Sat:", #PB_Text_Right)
SpinGadget(#SatSpin, 180, 50, 50, 20, 0, 100, #PB_Spin_Numeric)
SetGadgetState(#SatSpin, 0)
TextGadget(#SatCanvas, 180, 70, 50, 4, "")
DisableGadget(#SatCanvas, #True)
TextGadget(#LightText, 130, 90, 40, 20, "Light:", #PB_Text_Right)
SpinGadget(#LightSpin, 180, 90, 50, 20, 0, 100, #PB_Spin_Numeric)
SetGadgetState(#LightSpin, 0)
TextGadget(#LightCanvas, 180, 110, 50, 4, "")
DisableGadget(#LightCanvas, #True)
TextGadget(#HTMLText, 100, 130, 70, 20, "HTML code:", #PB_Text_Right)
StringGadget(#HTMLString, 180, 130, 50, 20, "")
CalcColors()
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
CalcColors()
Case #PB_Event_CloseWindow
Exit = #True
EndSelect
Until Exit