' Color picker dialog with scrollbars, preview via Win32 DECLARE
#COMPILE EXE
#DIM ALL

FUNCTION RGBFunc(BYVAL r AS LONG, BYVAL g AS LONG, BYVAL b AS LONG) AS LONG
    IF r > 255 THEN r = 255
    IF r < 0 THEN r = 0
    IF g > 255 THEN g = 255
    IF g < 0 THEN g = 0
    IF b > 255 THEN b = 255
    IF b < 0 THEN b = 0
    FUNCTION = r OR (g * 256) OR (b * 65536)
END FUNCTION

FUNCTION GetRed(BYVAL color AS LONG) AS LONG
    FUNCTION = color AND 255
END FUNCTION

FUNCTION GetGreen(BYVAL color AS LONG) AS LONG
    FUNCTION = (color / 256) AND 255
END FUNCTION

FUNCTION GetBlue(BYVAL color AS LONG) AS LONG
    FUNCTION = (color / 65536) AND 255
END FUNCTION

SUB ColorToHSL(BYVAL r AS LONG, BYVAL g AS LONG, BYVAL b AS LONG, BYREF h AS DOUBLE, BYREF s AS DOUBLE, BYREF l AS DOUBLE)
    LOCAL rf  AS DOUBLE
    LOCAL gf  AS DOUBLE
    LOCAL bf  AS DOUBLE
    LOCAL mx  AS DOUBLE
    LOCAL mn  AS DOUBLE
    LOCAL d  AS DOUBLE
    rf = r / 255.0
    gf = g / 255.0
    bf = b / 255.0
    mx = rf
    mn = rf
    IF gf > mx THEN mx = gf
    IF bf > mx THEN mx = bf
    IF gf < mn THEN mn = gf
    IF bf < mn THEN mn = bf
    l = (mx + mn) / 2.0
    IF mx = mn THEN
        h = 0.0
        s = 0.0
    ELSE
        d = mx - mn
        IF l > 0.5 THEN s = d / (2.0 - mx - mn) ELSE s = d / (mx + mn)
        IF mx = rf THEN h = ((gf - bf) / d + 6.0) * 60.0
        IF mx = gf THEN h = ((bf - rf) / d + 2.0) * 60.0
        IF mx = bf THEN h = ((rf - gf) / d + 4.0) * 60.0
        IF h > 360.0 THEN h = h - 360.0
    END IF
END SUB

SUB SimulateColorPicker()
    PRINT "Color Picker dialog simulation:"
    PRINT "  Three scrollbars: R(0-255) G(0-255) B(0-255)"
    LOCAL r  AS LONG
    LOCAL g  AS LONG
    LOCAL b  AS LONG
    LOCAL color  AS LONG
    r = 128
    g = 64
    b = 200
    color = RGBFunc(r, g, b)
    PRINT "  R="; r; " G="; g; " B="; b; " -> color="; color
    LOCAL hexStr AS STRING
    hexStr = "#" + RIGHT$("0" + HEX$(r, 2), 2) + RIGHT$("0" + HEX$(g, 2), 2) + RIGHT$("0" + HEX$(b, 2), 2)
    PRINT "  Hex: "; hexStr
    PRINT ""
    PRINT "  Scrolling R up by 20 increments:"
    LOCAL i AS LONG
    r = 128
    FOR i = 1 TO 5
        r = r + 20
        IF r > 255 THEN r = 255
        color = RGBFunc(r, g, b)
        PRINT "    R="; r; " G="; g; " B="; b; " color="; color
    NEXT i
END SUB

SUB PrintPalette()
    PRINT "Color palette samples:"
    DIM cr(0:7) AS LONG
    DIM cg(0:7) AS LONG
    DIM cb(0:7) AS LONG
    cr(0) = 255: cg(0) = 0: cb(0) = 0
    cr(1) = 0: cg(1) = 255: cb(1) = 0
    cr(2) = 0: cg(2) = 0: cb(2) = 255
    cr(3) = 255: cg(3) = 255: cb(3) = 0
    cr(4) = 0: cg(4) = 255: cb(4) = 255
    cr(5) = 255: cg(5) = 0: cb(5) = 255
    cr(6) = 255: cg(6) = 255: cb(6) = 255
    cr(7) = 0: cg(7) = 0: cb(7) = 0
    LOCAL i AS LONG
    FOR i = 0 TO 7
        LOCAL c AS LONG
        c = RGBFunc(cr(i), cg(i), cb(i))
        LOCAL h  AS DOUBLE
        LOCAL s  AS DOUBLE
        LOCAL l  AS DOUBLE
        ColorToHSL(cr(i), cg(i), cb(i), h, s, l)
        PRINT "  Color "; i; ": RGB("; cr(i); ","; cg(i); ","; cb(i); ") color="; c
    NEXT i
END SUB

SUB PreviewMixing()
    PRINT "Color mixing:"
    PRINT "  Red + Green = "; RGBFunc(255, 255, 0)
    PRINT "  Red + Blue = "; RGBFunc(255, 0, 255)
    PRINT "  Green + Blue = "; RGBFunc(0, 255, 255)
    PRINT "  All + White = "; RGBFunc(192, 192, 192)
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=== Color Picker Demo ==="
    PRINT "Win32: ScrollBar, FillRect via DECLARE"
    SimulateColorPicker()
    PrintPalette()
    PreviewMixing()
    PRINT "=== End Color Picker ==="
    FUNCTION = 0
END FUNCTION
