RGB 值到 HSL 转换器

发布于 2024-09-14 07:05:21 字数 572 浏览 1 评论 0原文

Google 地图 API v3 允许将“样式”应用于地图,包括设置各种要素的颜色。但是,它使用的颜色格式是 HSL(或者看起来像它的格式):

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

(来自docs

我设法找到了 RGB 到 HSL 转换器在线,但我不确定如何以谷歌地图接受的方式指定转换后的值。例如,转换器给出的典型 HSL 值将是:209° 72% 49%

该 HSL 值如何映射到我从 google 地图 API 指定的参数?即色调度值如何映射到 RGB 十六进制字符串以及百分比如何映射到 -100 到 100 之间的浮点值?

我仍然不确定如何进行转换。我需要,给定一个 RGB 值,快速将其转换为谷歌地图期望的值,以便颜色相同......

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

  • hue (an RGB hex string)
  • lightness (a floating point value between -100 and 100)
  • saturation (a floating point value between -100 and 100)

(from the docs)

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%

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2024-09-21 07:05:21

由于色调参数需要 RGB,因此您可以使用原始颜色作为色调。

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)

结果:

下面的屏幕截图显示了设置为 rgb 背景颜色的主体(在本例中为#2800E2),以及带有样式道路几何形状的谷歌地图,使用计算的值如上所述('0x2800E2',100,-11)。

很明显,谷歌使用您的样式创建以给定颜色为中心的大约六种不同的颜色,其中轮廓最接近输入。我相信这已经是最接近的了。

alt text


来自实验:http://gmaps-samples-v3.googlecode.com/svn/trunk/styledmaps/wizard/index.html

对于水,gmap减去 0.5 的伽玛值。要获得您想要的确切颜色,请使用上面的计算,然后添加 0.5 伽马值。

喜欢:

{
  featureType: "water",
  elementType: "geometry",
  stylers: [
    { hue: "#2800e2" },
    { saturation: 100 },
    { lightness: -11 },
    { gamma: 0.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.

alt text


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 },
  ]
}
九局 2024-09-21 07:05:21

我们编写了一个正是您想要的工具。它采用十六进制 RGB 值并生成所需的 HSL 代码。它带有预览和 Google Map JavaScript API V3 代码输出。尽情享受 ;D

http://googlemapscolorizr.stadtwerk.org/

We coded a tool which exactly does want you want. It takes hexadecimal RGB values and generates the needed HSL code. It comes with a preview and Google Map JavaScript API V3 code output. Enjoy ;D

http://googlemapscolorizr.stadtwerk.org/

少女情怀诗 2024-09-21 07:05:21

从链接页面:

注意:虽然色调采用 HTML 十六进制颜色值,但它仅使用该值来确定基本颜色(其在色轮周围的方向),而不是其饱和度或亮度,它们分别以百分比变化表示。例如,纯绿色的色调可以在色调属性中定义为“#00ff00”或“#000100”,并且两种色调将相同。 (这两个值都指向 HSL 颜色模型中的纯绿色。)由等量的红色、绿色和蓝色组成的 RGB 色调值 — 例如“#000000”(黑色)和“#FFFFFF”(白色)以及所有纯色调灰色 — 不指示任何色调,因为这些值都不指示 HSL 坐标空间中的方向。要表示黑色、白色或灰色,您必须删除所有饱和度(将值设置为 -100)并调整亮度。

至少在我读到的时候,这意味着您需要根据色轮转换角度。例如,假设 0 度为纯红色,120 度为纯蓝色,240 度为纯绿色。然后,您需要确定角度,找出它位于哪两个原色之间,并进行插值以确定每个原色要使用多少。从理论上讲,您可能应该使用二次插值 - 但很可能您可以使用线性插值相当好。

使用它,90 度(例如)是从红色到蓝色的 90/120 = 3/4ths,因此色调的十六进制数将为 0x00010003 - 或任何其他数字将绿色设置为 0,红色和蓝色之间的比例为 1:3。

From the linked page:

Note: while hue takes an HTML hex color value, it only uses this value to determine the basic color (its orientation around the color wheel), not its saturation or lightness, which are indicated separately as percentage changes. For example, the hue for pure green may be defined as "#00ff00" or "#000100" within the hue property and both hues will be identical. (Both values point to pure green in the HSL color model.) RGB hue values which consist of equal parts Red, Green and Blue — such as "#000000" (black) and "#FFFFFF" (white) and all the pure shades of grey — do not indicate a hue whatsoever, as none of those values indicate an orientation in the HSL coordinate space. To indicate black, white or grey, you must remove all saturation (set the value to -100) and adjust lightness instead.

At least as I read it, that means you need to convert your angle based on a color wheel. For example, let's assume 0 degrees is pure red, 120 degrees is pure blue and 240 degrees is pure green. You'd then take your angle, figure out which two primaries it falls between, and interpolate to determine how much of each primary to use. In theory you should probably use a quadratic interpolation -- but chances are that you can get by reasonably well with linear.

Using that, 90 degrees (for example) is 90/120 = 3/4ths of the way from red to blue, so your hex number for the hue would be 0x00010003 -- or any other number that had green set to 0, and a 1:3 ratio between red and blue.

快乐很简单 2024-09-21 07:05:21

我需要精确匹配颜色。所以我使用@stadt.werk提供的工具(http://googlemapscolorizr.stadtwerk.org/)来接近。

但后来我遇到了 @bukzor 解释的问题,其中 Google Maps API 在你的阴影上创建了变化,但这些变化似乎都不完全是我指定的。

所以我在浏览器中打开了地图,只截取了两个色调不太匹配的区域的屏幕截图,在图像编辑器中将其打开(在我的例子中是 pixlr.com),使用了 color-sucker工具来获取阴影的饱和度和亮度,将 Google API 调用中的饱和度和/或亮度调整为 1,然后重复,直到得到看起来完美匹配的东西。

当然,Google Maps API 可能会在不同的设备/浏览器等上对颜色执行不同的操作,但到目前为止,效果还不错。

是的,很乏味,但它有效。

I needed to match colors exactly. So I used the tool that @stadt.werk offers (http://googlemapscolorizr.stadtwerk.org/) to get close.

But then I ran into the problem explained by @bukzor where the Google Maps API creates variations on your shade, none of which seem to be exactly what I specified.

So I pulled up the map in a browser, took a screenshot of just the area with the two shades that weren't quite matching, opened it up in an image editor (pixlr.com, in my case), used the color-sucker tool to get the saturation and lightness for the shade, adjusted my saturation and/or lightness in the Google API call by 1, and repeated until I got something that seems to match perfectly.

It is possible, of course, that Google Maps API will do different things with the colors on different devices/browsers/etc., but so far, so good.

Tedious, yes, but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文