RGB值HSL转换器 [英] RGB value to HSL converter

查看:238
本文介绍了RGB值HSL转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谷歌地图API第3允许样式,以被应用到地图,包括设置的各种特征的颜色。但是,它使用的颜色格式是HSL(或什么似乎喜欢它):

Google maps api v3 allows "styles" to be applied to the map, including setting the color of various features. However, the color format it uses is HSL (or what seems like it):

  • 色相(一个RGB十六进制字符串)
  • 在亮度(-100到100之间的浮点值)
  • 在饱和度(-100到100之间的浮点值)

(从文档

我设法找到RGB到HSL转换器上网,但是我不能确定如何指定的方式,谷歌地图将接受转换后的值。例如,通过一个转换器所提供的典型HSL值是: 209°72%49%

I managed to find RGB to HSL converters online, but I am unsure how to specify the converted values in a way that google maps will accept. For instance, a typical HSL value given by a converter would be: 209° 72% 49%

这是如何HSL值映射到我从谷歌地图API指定的参数?即如何做一个色相度值映射到RGB十六进制字符串,以及如何做了个图-100和100之间的浮点值?

How does that HSL value map to the parameters I specified from the google maps api? i.e. how does a hue degree value map to an RGB hex string and how does a percentage map to a floating point value between -100 and 100?

我仍然不确定该怎么做转换。我需要,给定一个RGB值,迅速地将它转换成什么谷歌地图预计,这样的颜色将是相同的......

I am still uncertain how to do the conversion. I need to, given an RGB value, quickly convert it to what google maps expects so that the color will be identical...

推荐答案

由于色调参数预计RGB,您可以用原来的颜色为色调。

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:])

例如:

$ ./rgb2hsl.py F0 FF FF
('0xF0FFFF', 100, 94)


结果:


Result:

下面是一个屏幕截图示出了身体设置为(在此情况下,#2800E2)一个RGB背景颜色,并与风格的道路几何形状的谷歌地图,使用如上计算的值('0x2800E2',100,-11)

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).

这是pretty的明确表示,谷歌使用您的样式来创建一个围绕六种不同的颜色集中在特定的颜色,与轮廓是最接近输入。我相信这是因为接近,因为它得到。

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.

从实验用:<一href="http://gmaps-samples-v3.google$c$c.com/svn/trunk/styledmaps/wizard/index.html">http://gmaps-samples-v3.google$c$c.com/svn/trunk/styledmaps/wizard/index.html

有关水,GMaps实现减为0.5的伽玛。为了得到你想要的精确的颜色,使用上面的计算,并添加0.5伽玛回来了。

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.

这样的:

{
  featureType: "water",
  elementType: "geometry",
  stylers: [
    { hue: "#2800e2" },
    { saturation: 100 },
    { lightness: -11 },
    { gamma: 0.5 },
  ]
}

这篇关于RGB值HSL转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆