Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Swaggre · May 06, 2014 at 10:40 AM · colorconvertrgb

HSV to RGB without EditorGUIUtility.HSVToRGB

I have hue in degrees and saturation and brightness in %. How can I convert HSV to RGB without using EditorGUIUtility.HSVToRGB?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Bunny83 · May 06, 2014 at 11:35 AM

Well you could just look at the wiki HSV article and simply learn how to convert between those two color spaces, or use this "adapted" / decompiled version of EditorGUIUtility.HSVToRGB ;)

 public static Color HSVToRGB(float H, float S, float V)
 {
     if (S == 0f)
         return new Color(V,V,V);
     else if (V == 0f)
         return Color.black;
     else
     {
         Color col = Color.black;
         float Hval = H * 6f;
         int sel = Mathf.FloorToInt(Hval);
         float mod = Hval - sel;
         float v1 = V * (1f - S);
         float v2 = V * (1f - S * mod);
         float v3 = V * (1f - S * (1f - mod));
         switch (sel + 1)
         {
         case 0:
             col.r = V;
             col.g = v1;
             col.b = v2;
             break;
         case 1:
             col.r = V;
             col.g = v3;
             col.b = v1;
             break;
         case 2:
             col.r = v2;
             col.g = V;
             col.b = v1;
             break;
         case 3:
             col.r = v1;
             col.g = V;
             col.b = v3;
             break;
         case 4:
             col.r = v1;
             col.g = v2;
             col.b = V;
             break;
         case 5:
             col.r = v3;
             col.g = v1;
             col.b = V;
             break;
         case 6:
             col.r = V;
             col.g = v1;
             col.b = v2;
             break;
         case 7:
             col.r = V;
             col.g = v3;
             col.b = v1;
             break;
         }
         col.r = Mathf.Clamp(col.r, 0f, 1f);
         col.g = Mathf.Clamp(col.g, 0f, 1f);
         col.b = Mathf.Clamp(col.b, 0f, 1f);
         return col;
     }
 }


Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Swaggre · May 06, 2014 at 06:59 PM 0
Share

Yes! thanks

avatar image Sebiu · Dec 30, 2014 at 02:01 PM 0
Share

Can you also post the RGBtoHSV method, please?

avatar image tanoshimi · Dec 30, 2014 at 02:42 PM 0
Share
 public static void RGBToHSV(Color rgbColor, out float H, out float S, out float V)
 {
     if (rgbColor.b > rgbColor.g && rgbColor.b > rgbColor.r)
     {
         EditorGUIUtility.RGBToHSVHelper(4f, rgbColor.b, rgbColor.r, rgbColor.g, out H, out S, out V);
     }
     else
     {
         if (rgbColor.g > rgbColor.r)
         {
             EditorGUIUtility.RGBToHSVHelper(2f, rgbColor.g, rgbColor.b, rgbColor.r, out H, out S, out V);
         }
         else
         {
             EditorGUIUtility.RGBToHSVHelper(0f, rgbColor.r, rgbColor.g, rgbColor.b, out H, out S, out V);
         }
     }
 }
 
 private static void RGBToHSVHelper(float offset, float do$$anonymous$$antcolor, float colorone, float colortwo, out float H, out float S, out float V)
 {
     V = do$$anonymous$$antcolor;
     if (V != 0f)
     {
         float num = 0f;
         if (colorone > colortwo)
         {
             num = colortwo;
         }
         else
         {
             num = colorone;
         }
         float num2 = V - num;
         if (num2 != 0f)
         {
             S = num2 / V;
             H = offset + (colorone - colortwo) / num2;
         }
         else
         {
             S = 0f;
             H = offset + (colorone - colortwo);
         }
         H /= 6f;
         if (H < 0f)
         {
             H += 1f;
         }
     }
     else
     {
         S = 0f;
         H = 0f;
     }
 }

avatar image Sebiu · Dec 31, 2014 at 12:23 PM 0
Share

Thanks a lot! :)

avatar image 686InSomNia686 · Jan 18, 2016 at 09:21 AM 1
Share

Thanks for the answer, you have a small mistake :

return new Color.black;

you need to put off "new"

return Color.black;

Best regards, Clément

avatar image Bunny83 686InSomNia686 · Jan 18, 2016 at 11:20 PM 0
Share

Yes, you're right ^^ Thanks for the hint, i fixed it.

avatar image
1

Answer by Eric5h5 · May 06, 2014 at 07:01 PM

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 07, 2014 at 03:18 AM 0
Share

That's a nice helper class

+1

avatar image ben.aten · Jul 01, 2015 at 06:48 AM 0
Share

Hi, I am working on a color mixing game and used HSVToRGB conversion functionality for that, its working fine some of the colors and not for others, could anyone help me to sort this issue.

Regards, Ben

avatar image ben.aten · Jul 01, 2015 at 06:49 AM 0
Share

using UnityEngine; using System.Collections;

public class HSVtoRGB2 : $$anonymous$$onoBehaviour {

 public Color resultedColor;

 public float Hue;
 public float Saturation;
 public float Value;

 public GameObject RedClrSelection;
 public GameObject BlueClrSelection;
 public GameObject YellowClrSelection;
 public GameObject BlackClrSelection;
 public GameObject WhiteClrSelection;

 public float[] HSVofRed;
 public float[] HSVofBlue;
 public float[] HSVofYellow;
 public float[] HSVofBlack;
 public float[] HSVofWhite;

 public float amtOfColorsAdded;

 private RaycastHit hit;
 private Ray ray;

 int c;


 void Start()
 {
     amtOfColorsAdded = 1;
 //    HSVToRGB1(Hue,Saturation,Value);
 }

 public void HSVToRGB1(float H, float S, float V)
 {

     if (S == 0f)
     {
         resultedColor =  new Color(V,V,V,1);
     }
     else if (V == 0f)
     {
         resultedColor =  new Color(0,0,0,1);
     }
     else
     {
         Color col = Color.black;
         float Hval = H * 6f;
         int sel = $$anonymous$$athf.FloorToInt(Hval);
         float mod = Hval - sel;
         float v1 = V * (1f - S);
         float v2 = V * (1f - S * mod);
         float v3 = V * (1f - S * (1f - mod));

         Debug.Log (sel);

         switch (sel + 1)
         {
             case 0:
                 col.r = V;
                 col.g = v1;
                 col.b = v2;
                 break;
             case 1:
                 col.r = V;
                 col.g = v3;
                 col.b = v1;
                 break;
             case 2:
                 col.r = v2;
                 col.g = V;
                 col.b = v1;
                 break;
             case 3:
                 col.r = v1;
                 col.g = V;
                 col.b = v3;
                 break;
             case 4:
                 col.r = v1;
                 col.g = v2;
                 col.b = V;
                 break;
             case 5:
                 col.r = v3;
                 col.g = v1;
                 col.b = V;
                 break;
             case 6:
                 col.r = V;
                 col.g = v1;
                 col.b = v2;
                 break;
             case 7:
                 col.r = V;
                 col.g = v3;
                 col.b = v1;
                 break;
         }
         col.r = $$anonymous$$athf.Clamp(col.r, 0f, 1f);
         col.g = $$anonymous$$athf.Clamp(col.g, 0f, 1f);
         col.b = $$anonymous$$athf.Clamp(col.b, 0f, 1f);
         //return col;
         resultedColor = new Color(col.r,col.g,col.b,1);

     }

     amtOfColorsAdded = 2;
     Camera.main.backgroundColor = resultedColor;

 }
avatar image ben.aten · Jul 01, 2015 at 06:50 AM 0
Share
 void Update()
 {
     if(Input.Get$$anonymous$$ouseButtonDown (0) && Input.touchCount < 2)
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);

         if (Physics.Raycast (ray, out hit, float.PositiveInfinity)) 
         {
             if(hit.collider.gameObject == RedClrSelection)
             {
                 Hue = (Hue + HSVofRed[0]) /amtOfColorsAdded;
                 Saturation = (Saturation + HSVofRed[1]) /amtOfColorsAdded;
                 Value = (Value + HSVofRed[2]) /amtOfColorsAdded;

                 HSVToRGB1(Hue, Saturation, Value);
             }
             else if(hit.collider.gameObject == BlueClrSelection)
             {
                 Hue = (Hue + HSVofBlue[0]) /amtOfColorsAdded;
                 Saturation = (Saturation + HSVofBlue[1]) /amtOfColorsAdded;
                 Value = (Value + HSVofBlue[2]) /amtOfColorsAdded;

                 HSVToRGB1(Hue, Saturation, Value);
             }
             else if(hit.collider.gameObject == YellowClrSelection)
             {
                 Hue = (Hue + HSVofYellow[0]) /amtOfColorsAdded;
                 Saturation = (Saturation + HSVofYellow[1]) /amtOfColorsAdded;
                 Value = (Value + HSVofYellow[2]) /amtOfColorsAdded;

                 HSVToRGB1(Hue, Saturation, Value);
             }
             else if(hit.collider.gameObject == BlackClrSelection)
             {
                 Hue = (Hue + HSVofBlack[0]) /amtOfColorsAdded;
                 Saturation = (Saturation + HSVofBlack[1]) /amtOfColorsAdded;
                 Value = (Value + HSVofBlack[2]) /amtOfColorsAdded;

                 HSVToRGB1(Hue, Saturation, Value);
             }
             else if(hit.collider.gameObject == WhiteClrSelection)
             {
                 Hue = (Hue + HSVofWhite[0]) /amtOfColorsAdded;
                 Saturation = (Saturation + HSVofWhite[1]) /amtOfColorsAdded;
                 Value = (Value + HSVofWhite[2]) /amtOfColorsAdded;

                 HSVToRGB1(Hue, Saturation, Value);
             }
             
         }
     }
 }

}

avatar image
0

Answer by anton-melegov · Jul 01, 2015 at 10:35 AM

 public static class ColorUtil
     {
         public static uint ToUint(Color32 color)
         {
             return (uint)((color.a << 24) | (color.r << 16) | (color.g << 8) | (color.b << 0));
         }
 
         public static Color32 FromUint(uint color)
         {
             return new Color32((byte)(color >> 16), (byte)(color >> 8), (byte)color, (byte)(color >> 24));
         }
 
         public static float[] ToHSV(Color32 color)
         {
             var r = color.r/255f;
             var g = color.g/255f;
             var b = color.b/255f;
 
             var min = Mathf.Min(r, g, b);
             var max = Mathf.Max(r, g, b);
             var delta = max - min;
             
             var h = 0f;
             var s = 0f;
             var v = max;
 
             if (delta.Equals(0f))
             {
                 return new[] {h, s, v};
             }
 
             s = delta/max;
 
             var dR = ((max - r)/6f + delta/2f)/delta;
             var dG = ((max - g)/6f + delta/2f)/delta;
             var dB = ((max - b)/6f + delta/2f)/delta;
 
             if (r.Equals(max))
             {
                 h = dB - dG;
             } 
             else if (g.Equals(max)) 
             {
                 h = 1f/3f + dR - dB;
             } 
             else if (b.Equals(max))
             {
                 h = 2f/3f + dG - dR;
             }
 
             if (h < 0)
             {
                 h += 1;
             }
             else if (h > 1)
             {
                 h -= 1;
             }
 
             return new[] {h, s, v};
         }
 
         public static Color32 FromHSV(float h, float s, float v)
         {
             if ( s.Equals(0f) )
             {
                 var normV = (byte) (v*255f);
                 return new Color32(normV, normV, normV, 255);
             }
 
             h = h.Equals(1f) ? 0f : h*6f;
 
             var i = (int)h;
 
             var r = v;
             var g = v;
             var b = v;
 
             switch (i)
             {
                 case 0:
                     g = v*(1f - s*(1f - (h - i)));
                     b = v*(1f - s);
                     break;
                 case 1:
                     r = v*(1f - s*(h - i)); 
                     b = v*(1f - s);
                     break;
                 case 2:
                     r = v*(1f - s); 
                     b = v*(1f - s*(1f - (h - i)));
                     break;
                 case 3:
                     r = v*(1f - s);
                     g = v*(1f - s*(h - i));
                     break;
                 case 4:
                     r = v*(1f - s*(1f - (h - i))); 
                     g = v*(1f - s);
                     break;
                 case 5:
                     g = v*(1f - s);
                     b = v*(1f - s*(h - i));
                     break;
             }
 
             return new Color32( (byte)(r * 255f), (byte)(g * 255f), (byte)(b * 255f), 255 );
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ben.aten · Jul 01, 2015 at 12:22 PM 0
Share

Hi, Thanks for the reply. I have to call FromHSV function alone or need to call other functions for color conversion.

Regards, Ben

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

convert rgb to cmyk 1 Answer

Convert Color32 to Color 1 Answer

How do I change colors of an axis? 1 Answer

Colors Seem all wrong when I set them in RGB 2 Answers

convert string to color 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges