"rgb value to hsl converter" Code Answer

5

since the hue argument expects rgb, you can use the original color as the hue.

rgb2hsl.py:

#!/usr/bin/env python

def rgb2hsl(r, g, b):
    #hue: the rgb string
    h = (r<<16) + (g<<8) + b
    h = "0x%06x" % h

    #convert to [0 - 1] range
    r = float(r) / 0xff
    g = float(g) / 0xff
    b = float(b) / 0xff

    #http://en.wikipedia.org/wiki/hsl_and_hsv#lightness
    m = max(r,g,b)
    m = min(r,g,b)
    c = m - m

    #lightness
    l = (m + m) / 2

    #saturation (hsl)
    if l == 0:
        s = 0
    elif l <= .5:
        s = c/(2*l)
    else:
        s = c/(2 - 2*l)

    #gmaps wants values from -100 to 100
    s = int(round(s * 200 - 100))
    l = int(round(l * 200 - 100))

    return (h, s, l)


def main(r, g, b):
    r = int(r, base=16)
    g = int(g, base=16)
    b = int(b, base=16)
    print rgb2hsl(r,g,b)

if __name__ == '__main__':
    from sys import argv
    main(*argv[1:])

example:

$ ./rgb2hsl.py f0 ff ff
('0xf0ffff', 100, 94)

result:

below is a screenshot showing the body set to a rgb background color (#2800e2 in this case), and a google map with styled road-geometry, using the values calculated as above ('0x2800e2', 100, -11).

it's pretty clear that google uses your styling to create around six different colors centered on the given color, with the outlines being closest to the input. i believe this is as close as it gets.


from experimentation with: http://gmaps-samples-v3.googlecode.com/svn/trunk/styledmaps/wizard/index.html

for water, gmaps subtracts a gamma of .5. to get the exact color you want, use the calculations above, and add that .5 gamma back.

like:

{
  featuretype: "water",
  elementtype: "geometry",
  stylers: [
    { hue: "#2800e2" },
    { saturation: 100 },
    { lightness: -11 },
    { gamma: 0.5 },
  ]
}
By Gerald Zehetner on April 30 2022

Answers related to “rgb value to hsl converter”

Only authorized users can answer the Search term. Please sign in first, or register a free account.