可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to convert a HSB Color to RGB. The way I am doing that is
System.Windows.Media.Color winColor = value; System.Drawing.Color drawColor = System.Drawing.Color.FromArgb(winColor.R, winColor.G, winColor.B); Hue = (byte)(drawColor.GetHue()*255); Saturation = (byte)(drawColor.GetSaturation()*255); Luminosity = (byte)(drawColor.GetBrightness()*255);
I find that when I have FF0000
, it will be converted to H = 0, S = 255, L = 127
which converts to RGB FF0E0E
. I think Luminosity should be 120? Or am I getting the whole HSB thing wrong? When I look at the color picker in Photoshop, Hue is 0-360 degrees, Saturation, Luminosity is 0-100%. I am having HSB values ranging from 0 - 255, am I doing it wrong?
回答1:
Maybe you already look into this wikipedia article, but to make it clear.
There is a difference between HSL and HSB (aka HSV).
So you can't take the (B)rightness from the color class and use it like a (L)uminosity.
To get back from the Color class provided values GetHue()
, GetSaturation()
and GetBrightness()
to a normal color you should give this extension method a chance.
/// /// Creates a Color from alpha, hue, saturation and brightness. /// /// The alpha channel value. /// The hue value. /// The saturation value. /// The brightness value. /// A Color with the given values. public static Color FromAhsb(int alpha, float hue, float saturation, float brightness) { if (0 > alpha || 255 hue || 360f saturation || 1f brightness || 1f
Update
So just to make things clear. My code above and the three methods within the Color class mentioned above are using the HSB (aka HSV) color model, but Photoshop uses the HSL color model.
In your comment you wrote that the parameters Hue = 0
, Saturation = 1
and Brightness = 1
give you with the code above a red color and white in Photoshop. When you take a closer look at the differences of these modes this makes absolutely sense:
The HSL cylinder

- In both models the hue acts as the same by using color Red as start and end point (zero and 360 degree).
- By just looking at the hue you've got a red color.
- The saturation defines how opaque the color is or how much the portion of a whitescale is.
- So by setting it to one you say you want a full shiny red color.
- The lighting now defines how much is the black and white part within your color. By setting it to zero you got black, one means white and 0.5 means perfect weighting.
- So by setting it to one you say you want it as bright as possible which result in a white color.
The HSB cylinder

- In both models the hue acts as the same by using color Red as start and end point (zero and 360 degree).
- By just looking at the hue you've got a red color.
- The saturation defines how opaque the color is or how much the portion of a whitescale is.
- So by setting it to one you say you want a full shiny red color.
- The brightness (or value) now defines how much is the black part within the color (not the white part).
- So by setting it to one you say you want it full colored leading to a full shiny red color.
As you can see, Photoshop and the .Net framework (including my extension function) are using different coloring models. So you should check if you find somewhere an implementation of the other coloring model, a transformation or something else that gives you the results you need.
回答2:
This works...modified from Java source code. The other answer is still HSL, This really is HSB to RGB (actually it's HSB/HSV to System.Windows.Media.Color as my return type)
public static Color HSBtoRGB(float hue, float saturation, float brightness) { int r = 0, g = 0, b = 0; if (saturation == 0) { r = g = b = (int)(brightness * 255.0f + 0.5f); } else { float h = (hue - (float)Math.Floor(hue)) * 6.0f; float f = h - (float)Math.Floor(h); float p = brightness * (1.0f - saturation); float q = brightness * (1.0f - saturation * f); float t = brightness * (1.0f - (saturation * (1.0f - f))); switch ((int)h) { case 0: r = (int)(brightness * 255.0f + 0.5f); g = (int)(t * 255.0f + 0.5f); b = (int)(p * 255.0f + 0.5f); break; case 1: r = (int)(q * 255.0f + 0.5f); g = (int)(brightness * 255.0f + 0.5f); b = (int)(p * 255.0f + 0.5f); break; case 2: r = (int)(p * 255.0f + 0.5f); g = (int)(brightness * 255.0f + 0.5f); b = (int)(t * 255.0f + 0.5f); break; case 3: r = (int)(p * 255.0f + 0.5f); g = (int)(q * 255.0f + 0.5f); b = (int)(brightness * 255.0f + 0.5f); break; case 4: r = (int)(t * 255.0f + 0.5f); g = (int)(p * 255.0f + 0.5f); b = (int)(brightness * 255.0f + 0.5f); break; case 5: r = (int)(brightness * 255.0f + 0.5f); g = (int)(p * 255.0f + 0.5f); b = (int)(q * 255.0f + 0.5f); break; } } return Color.FromArgb(Convert.ToByte(255), Convert.ToByte(r), Convert.ToByte(g), Convert.ToByte(b)); }