Color conversion

Hi all,

Does anybody know how to convert Colors from RGB values to HSB?

  • I have a list of Colors setup which I tween among and assign to the glowTint,
    and I need to apply a lighter version of the same color to a material.

The idea was to get the HSB value of the color, lighten it, and use it.

Thanks in advance,

Jørgen

Wouldn’t just multiplying the color by some larger-than-1 number do effectively the same? (i.e. multiply by two - get twice lighter color)

This doesn’t work in the general case because the three primaries don’t contribute equally to the overall luminance (red=29.9%, green=58.7%, blue=11.4%). Also, changing the luma usually alters the saturation too. For example, if the red and green were zero and the blue was 100% then multiplying the vector by two and then scaling would give exactly the same colour you started with. However, you can change the luma for this hue if you accept a loss of saturation and raise the red and green equally.

This Wikipedia page contains the relevant calculations. Note that it is actually the HSL colour model rather than the HSB/HSV that you want if you are altering the “brightness” (ie, luminance) of a colour. The term “brightness” is used inconsistently in the name of the HSB model, unfortunately.

Hello,

http://www.unifycommunity.com/wiki/index.php?title=HSBColor

That’ll allow you to convert between Color and HSBColor, and some bonus stuff. Enjoy! :slight_smile:

-Jon

hmm. Thanks for the information.

There seems to be a (whacky?) implementation in C# - Color.GetBrightness() for example - but Unity’s Color class does not seem to implement those functions.

j.

awesome!
Thanks a lot Jonathan.

Not wanting to be pedantic, but the wiki code is for the HSB model and I think the HSL model is what is needed here. The “brightness” under HSB is not actually the perceived screen brightness but rather the largest of the three primary values. For example, pure blue (r=0, g=0, b=100%) has 100% brightness in the HSB model. White also has 100% brightness, but in actual fact pure blue has only about 11% of the luminance of white as far as the viewer is concerned.

If you are looking for a glow effect (fringe of lighter colour around the object?) then you should be aware that none of the pure primaries and secondaries (magenta, cyan, yellow) can be “brightened” in the HSB model. Objects in these colours presumably won’t glow as you would expect.

I may just be misunderstanding what you want to do here, in which case I apologise. But I think you might be surprised by the results you will get from using HSB rather than HSL - they really are significantly different colour models.

andeeee, thanks for your precision - would you be able to create a class like Jonathan’s implementing this?

FYI - what I was looking to accomplish in my current scene was simply to have one base color affect camera.bg, fog.color and, with a bit more brightness, the object(s) itself.

j.

Attached is a class that is similar to Jonathan’s HSBColor but it implements the HSL model instead. I’ve added implicit type casting operators (so that you can simply assign an HSLColor to a Color and vice versa) but it’s OK to use redundant explicit casts where it clarifies the code.

On re-reading my earlier posts, I can see that they might come across a bit abrasive. I apologise for that - I certainly didn’t intend to suggest that there was anything wrong with the HSB model (or JC’s implementation, which I used as the basis for my code). What I should have said is that HSB is optimised for colour pickers whereas HSL behaves itself slightly better when used as a colour adjustment (eg, Photoshop uses HSB for the colour picker, but HSL for the Hue/Saturation command).

44983–1637–$hsltest_822.cs (720 Bytes)
44983–1638–$hslcolor_211.cs (2.15 KB)

excellent. thanks again.

I don’t understand how to use this. When I try to drag it onto a game object it says it won’t work because it has no monobehaviour. The install instruction says I have to make a folder called “Plugins” and put it in there to call it via java script. I am only working in c# and don’t know java script commands on how to call a plugin script.

I wanted a given RGB color information be converted to a HSB color. How do I install the script correctly? :face_with_spiral_eyes:

You don’t drag it onto a game object. If you’re using C# then you don’t necessarily need to put it in plugins (although putting it there or Standard Assets is probably a good idea anyway, that way it won’t need to recompile when scripts outside Standard Assets are recompiled). It’s a class; you use it like any other class…var col = new HSBColor(Color.black).

–Eric

Ah thank you, now I understand how this works! :slight_smile:

how do i call the 3 functions from unityscript? i.e. public static HSBColor FromColor(Color color)

You can also use the Unity code if you are on runtime

I don’t understand why this is in UnityEditor and not in UnityEngine :confused:

1 Like