Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Color conversion

Discussion in 'Scripting' started by Jorgen, Aug 11, 2007.

  1. Jorgen

    Jorgen

    Joined:
    Dec 13, 2005
    Posts:
    38
    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
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,590
    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)
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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.
     
  4. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
  5. Jorgen

    Jorgen

    Joined:
    Dec 13, 2005
    Posts:
    38
    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.
     
  6. Jorgen

    Jorgen

    Joined:
    Dec 13, 2005
    Posts:
    38
    awesome!
    Thanks a lot Jonathan.
     
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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.
     
  8. Jorgen

    Jorgen

    Joined:
    Dec 13, 2005
    Posts:
    38
    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.
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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).
     

    Attached Files:

  10. Jorgen

    Jorgen

    Joined:
    Dec 13, 2005
    Posts:
    38
    excellent. thanks again.
     
  11. olop01

    olop01

    Joined:
    Jul 5, 2012
    Posts:
    14
    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? :confused:
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,238
    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
     
  13. olop01

    olop01

    Joined:
    Jul 5, 2012
    Posts:
    14
    Ah thank you, now I understand how this works! :)
     
  14. reefwirrax

    reefwirrax

    Joined:
    Sep 20, 2013
    Posts:
    138
    how do i call the 3 functions from unityscript? i.e. public static HSBColor FromColor(Color color)
     
  15. Sylafrs

    Sylafrs

    Joined:
    Jun 25, 2013
    Posts:
    31
    You can also use the Unity code if you are on runtime

    http://pastebin.com/wxr9TUKf

    I don't understand why this is in UnityEditor and not in UnityEngine :/
     
    MikeGameDev likes this.
unityunity