Search Unity

Change color hue by code? (not saturation, not brightness)

Discussion in 'Scripting' started by Shushustorm, Oct 10, 2015.

Thread Status:
Not open for further replies.
  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Hello everyone!
    Is there a way to change the hue of a color by code?

    Thanks for any ideas,
    Greetings,
    Shu
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Most (but not all) materials have a .color property.

    Generally you can set this field to any color you want with a call like this, assuming this script is ON the object where the renderer is.

    Code (csharp):
    1.  gameObject.GetComponent<Renderer>().material.color = new Color( 1.0f, 1.0f, 0.0f); // yellow
     
    BitGamey and Shushustorm like this.
  3. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Thanks for your answer!
    I am actually referring to something different, though:
    I would like to change hue only, not saturation or brightness.
    Basically, I am looking for something like that:
    Code (CSharp):
    1. Color SomeColor1 = Color.blue;
    2.  
    3. void Start () {
    4. SomeColor1.hue += 60;
    5. // SomeColor1 should be pink now, with its brightness and saturation still at 100%
    6.  
    7. SomeColor1.brightness -= 0.5f;
    8. // SomeColor1 should have lost half of its brightness
    9.  
    10. SomeColor1.saturation -= 0.5f;
    11. // SomeColor1 should have lost half of its saturation
    12. }
    But from what I have read, there is no Color.hue, nor Color.brightness or Color.saturation.
    Maybe I have to use Color.greyscale to ship around this problem somehow. But that information only includes brightness. I don't know.
     
    Last edited: Oct 10, 2015
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Convert RGB to HSV, change it, and convert back.
    Theres alot of results on google for this
     
    Shushustorm likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Yes, exactly what @hpjohn said... Unity's Color constructor works in RGB space, you want to traverse the H axis of HSV space...

    NOW... there is slightly-cheaper way you can "color bend" materials: when you set the .color property, you are effectively telling the shader to multiply all textures by that color before rendering. If the texture is grayscale, then you are getting a "tint" effect for free.

    However, if your texture has more than one color in it, things can get tricky. Red times green is black, red times blue is black, etc. Yellow times Red is red... things like that. Each channel (R,G,B) is multiplied individually.
     
    Shushustorm likes this.
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  7. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    I see. Conversion is harder than I thought, though.
    The only possibility I find within Unity is this:
    http://docs.unity3d.com/ScriptReference/EditorGUIUtility.RGBToHSV.html
    But it's for the Editor.
    The options presented here aren't very attractive either:
    http://stackoverflow.com/questions/359612/how-to-change-rgb-color-to-hsv
    Some options seem to not include conversion back.

    Thanks! I will check it out.

    EDIT:
    I guess I cannot take it from there because of "
    ".
     
    Last edited: Oct 10, 2015
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Why would the Creative Commons Attribute Share Alike license disallow you from using it?

    All you have to do is include attribution.
     
  9. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    I interpreted the second term ( on http://creativecommons.org/licenses/by-sa/3.0/ ) so that I have to distribute any app that includes the script for free, because I got the script for free, too, and I "build upon" the material.
    Besides, in my case it's probably quicker to set the values in the Editor by hand than to add menus to all apps that point out the source of the script.
    I just thought there might be a quick way to do this in Unity by accessing values directly.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    The second term was specifically stating you're free to use it how ever you want, for free, no cost to you. Just include attribution.


    Also the colour values are just a number in 32-bit ARGB.

    ARGB does not directly encode hue.

    So yeah, you'd have to convert it. Even if unity gave you a 'hue' property, it would essentially be converting it for you. Because computer graphics devices don't usually work in HSV, they work in RGB.
     
    Shushustorm likes this.
  11. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    i added the second hsv function version on teh wiki. I done that alot. you can either do it on the shader which is crazy fast, a bit less precise than 32 bit, and 1000 FPS, or you can change materials and textures directly, which can be efficient in some scenarios and is easier than shader and more precise colors, you have to use HSV indeed.
     
    Shushustorm likes this.
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    @Shushustorm - if you are concerned about authors and what not since you don't really know who wrote that one.

    Here:
    https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBase/ColorHSV.cs

    I had my own ColorUtil that extracted HSV, but I never actually made it a data structure.

    So I implemented it today, its available under MIT license, which basically means (and this is from my mouth, the author)... you can use it however the heck you want, I really don't care, that's why my repo is public.

    I also included a few extra bells and whistles. Operators, implicit and explicit conversion, normalize method, etc.
     
    Last edited: Oct 13, 2015
  13. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Thanks a lot! This is awesome!
     
    lyingturkey likes this.
  14. Lohoris2

    Lohoris2

    Joined:
    Aug 20, 2013
    Posts:
    85
    Awesome, thanks!
     
  15. AuggoDoggo

    AuggoDoggo

    Joined:
    Jul 12, 2015
    Posts:
    3
    Hey, I know this is an old post, but it was the first one I found while searching for a similar solution. I was trying to bring in a custom RGBtoHSV conversion when I realized that Unity has it's own function "Color.RGBToHSV();" and "Color.HSVToRGB();"

    I think they're what you're looking for. I'm going to continue to use it for my project and if there happens to be any problem with this down the line i'll update. Hope this helps someone who comes across this in Unity 5.5 (2017).
     
    lyingturkey and Kurt-Dekker like this.
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Those functions didn't exist until Unity 5.3.

    --Eric
     
    Shushustorm and lyingturkey like this.
  17. RomanCalderon

    RomanCalderon

    Joined:
    Jan 4, 2014
    Posts:
    6
    umm last time i checked, he didn't ask for saturation and brightness..
     
Thread Status:
Not open for further replies.