Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
2
Question by jk24777 · Nov 23, 2015 at 12:53 PM · c#colorhexrgbrgba

How to get the Color Code in RGB Hex from RGBA? (Unity 3D 5.0f1)

I actually got stuck at this point where I'm doing the following and to get color code in RGB Hex from Unity Color

  1. loading an image from camera into a RawImage.texture as Texture2D which displays just fine.(there is no material set to RawImage but it has solid white color in the start)

  2. then i am using my touched position(using Input.GetButtonDown("Fire1") and Input.mousePosition ) on the image as coordinates into my_texture.GetPixel(x,y);

  3. But the color code convertion which i applied gave me always #010101 which translates to black on online color converters.

                 Texture2D my_texture = RawImageEnlarge.texture as Texture2D;
         
                 int x = (int)my_position.x;
                 int y = (int)my_position.y;
         
                 Color my_color = my_texture.GetPixel(x, y);
                 //TODO must remove after testing ------
         
                 //blend alpha : converting argb to rgb 
                 Color blend = Color.white; 
                 int alpha = (int)my_color.a;
                 int r = (int)((alpha/255)*my_color.r + (1 - alpha/255)*blend.r);
                 int g = (int)((alpha/255)*my_color.g + (1 - alpha/255)*blend.g);
                 int b = (int)((alpha/255)*my_color.b + (1 - alpha/255)*blend.b);
         
         
                 string hexCC = "#"+r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
         
         //OR //string hexCC = UnityEngine.ColorUtility.ToHtmlStringRGB(my_color); //unity does not recognize ColorUtility class :/ 
    
    
    
    
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

4 Replies

· Add your reply
  • Sort: 
avatar image
30

Answer by franktinsley · Mar 02, 2017 at 01:02 AM

You guys are all trying way too hard. Just use ColorUtility.ToHtmlStringRGB( myColor ) and get on with it.

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 PizzaProgram · Apr 05, 2017 at 04:31 PM 2
Share

To be exact: ColorUtility.ToHtmlStringRGBA( myColor ) RGBA ... not just RGB :)

avatar image MI_Buddy PizzaProgram · Oct 20, 2017 at 04:46 PM 4
Share

For others who come by, they're both options:

RGB: https://docs.unity3d.com/ScriptReference/ColorUtility.ToHtmlStringRGB.html

RGBA: https://docs.unity3d.com/ScriptReference/ColorUtility.ToHtmlStringRGBA.html

avatar image mrCrush · Feb 03, 2018 at 12:15 PM 1
Share

Thanks a lot!

avatar image Zengon · Nov 13, 2019 at 09:20 PM 1
Share

Looked everywhere and all I've been seeing is confusing methods, formulas and conversions. You're a life saver, thank you!

avatar image
6

Answer by ExamplesAreExamples · Nov 24, 2015 at 11:11 PM

So, here's a basic example that may work. I have not tested all possible values of color channels, but I tested each channel, not a number, positive and negative infinity, alpha etc, to make sure the output stays within #000000 to #FFFFFF and doesn't throw exceptions. You may wish different behaviour for negative, positive infinity or not a number cases perhaps, but this is up to what makes sense to you. In most cases, you won't be feeding those values into any channel anyway.

 using UnityEngine;
 
 public static class ColorTypeConverter
 {
     public static string ToRGBHex(Color c)
     {
         return string.Format("#{0:X2}{1:X2}{2:X2}", ToByte(c.r), ToByte(c.g), ToByte(c.b));
     }
 
     private static byte ToByte(float f)
     {
         f = Mathf.Clamp01(f);
         return (byte)(f * 255);
     }
 }

And the tests for reference.

 using NUnit.Framework;
 using UnityEngine;
 
 [TestFixture]
 [Category("Type Conversion")]
 internal class ColorTypeConverterTests
 {
     [Test]
     public void ConvertsCommonColors()
     {
         Assert.AreEqual("#000000", ColorTypeConverter.ToRGBHex(Color.black));
         Assert.AreEqual("#FFFFFF", ColorTypeConverter.ToRGBHex(Color.white));
     }
 
     [Test]
     public void IgnoresAlphaChannel()
     {
         Assert.AreEqual("#FFFFFF", ColorTypeConverter.ToRGBHex(new Color(1, 1, 1, 0)));
         Assert.AreEqual("#FFFFFF", ColorTypeConverter.ToRGBHex(new Color(1, 1, 1, 1)));
     }
 
     [Test]
     public void ConvertsChannelsInRGBOrder()
     {
         Assert.AreEqual("#FF0000", ColorTypeConverter.ToRGBHex(Color.red));
         Assert.AreEqual("#00FF00", ColorTypeConverter.ToRGBHex(Color.green));
         Assert.AreEqual("#0000FF", ColorTypeConverter.ToRGBHex(Color.blue));
     }
 
     [Test]
     public void ConvertsYellow()
     {
         UnityYellowIsKnown();
         Assert.AreEqual("#FFEB04", ColorTypeConverter.ToRGBHex(Color.yellow));
     }
 
     [Test]
     public void UnityYellowIsKnown()
     {
         // ConvertsYellow use yellow color. If Unity changes the definition of 
         // yellow, those tests will fail. To spare developer confusion about 
         // failing tests, assert that the channels are known. Because the channels
         // are so arbitrary, it's also nice to see the floats in plain text.
         Assert.AreEqual(1, Color.yellow.r, "Color.yellow.r changed");
         Assert.AreEqual(0.921568632f, Color.yellow.g, "Color.yellow.g changed");
         Assert.AreEqual(0.0156862754f, Color.yellow.b, "Color.yellow.b changed");
     }
 
     [Test]
     public void ClampsChannelsBetweenZeroAndOne()
     {
         Assert.AreEqual("#FFFFFF", ColorTypeConverter.ToRGBHex(PosInf()));
         Assert.AreEqual("#FFFFFF", ColorTypeConverter.ToRGBHex(Fill(1.5f)));
         Assert.AreEqual("#FFFFFF", ColorTypeConverter.ToRGBHex(Fill(1)));
         Assert.AreEqual("#000000", ColorTypeConverter.ToRGBHex(Fill(0)));
         Assert.AreEqual("#000000", ColorTypeConverter.ToRGBHex(Fill(-0.5f)));
         Assert.AreEqual("#000000", ColorTypeConverter.ToRGBHex(NegInf()));
     }
 
     [Test]
     public void NotANumberConvertsToBlack()
     {
         Assert.AreEqual("#000000", ColorTypeConverter.ToRGBHex(NaN()));
     }
 
     private static Color NaN()
     {
         return Fill(float.NaN);
     }
 
     private static Color NegInf()
     {
         return Fill(float.NegativeInfinity);
     }
 
     private static Color PosInf()
     {
         return Fill(float.PositiveInfinity);
     }
 
     private static Color Fill(float c)
     {
         return new Color(c, c, c, c);
     }
 }
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 SweatyChair · Aug 16, 2016 at 05:27 AM 0
Share

great answer with excellent unit tests!

avatar image
0

Answer by jk24777 · Nov 24, 2015 at 04:07 PM

I was able to get this working but I think its not giving me correct color code. i cant patch it up. Help highly apreciated!

         Texture2D t2d = RawImageEnlarge.texture as Texture2D;
         int x = (int)ClickedPosition.x;
         int y = (int)ClickedPosition.y;
         //(RawImageEnlarge.texture as Texture2D).format = TextureFormat.RGB24;
 
         Color my_color = (t2d).GetPixel(x, y);
         Color32 myyyy_color = my_color;
         Debug.Log ("MYY COLOR: "+my_color);
         Debug.Log ("MYYYYYY COLOR: "+myyyy_color);
         //TODO must remove after testing ------
 
         //blend alpha : converting argb to rgb 
         Color blend = Color.white; 
         int alpha = (int)myyyy_color.a;
         int r = (int)((alpha/255)*myyyy_color.r + (1 - alpha/255)*blend.r);
         int g = (int)((alpha/255)*myyyy_color.g + (1 - alpha/255)*blend.g);
         int b = (int)((alpha/255)*myyyy_color.b + (1 - alpha/255)*blend.b);
 
         //                        OR                       // now working atm
         //ColorTranslator.ToHtml (Color.FromArgb (Color.Tomato.ToArgb ()));
 
         string hexCC = "#"+r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
         
             //string hexCC = ColorUtility.ToHtmlStringRGB(my_color);
         
         ColorCodeCC.text = hexCC;
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 ExamplesAreExamples · Nov 24, 2015 at 09:37 PM 0
Share

Extract the color/hexadecimal conversion code into standalone function.

Then write a unit test for the conversion function.

$$anonymous$$ake the unit test simple, like

 // Test that white is returned as #FFFFFF
 Assert.AreEqual("#FFFFFF", ColorConverter.ToRGBHex(new Color(1, 1, 1, 0));
 
 // Test that alpha doesn't affect basic output
 Assert.AreEqual("#000000", ColorConverter.ToRGBHex(new Color(0, 0, 0, 1));
 Assert.AreEqual("#FFFFFF", ColorConverter.ToRGBHex(new Color(1, 1, 1, 1));
 
 // Test that components go into the right characters in the string
 Assert.AreEqual("#FF0000", ColorConverter.ToRGBHex(new Color(1, 0, 0, 0));
 Assert.AreEqual("#00FF00", ColorConverter.ToRGBHex(new Color(0, 1, 0, 0));
 Assert.AreEqual("#0000FF", ColorConverter.ToRGBHex(new Color(0, 0, 1, 0));

 // I am sure you can think of more tests, as you go along or if you find bugs.

Feel free to break it up into separate tests if you don't like to have them all in one method.

You can get Unity Test Tools for free from the Asset Store. It's okay, but I recommend you remove the examples after giving it a spin, because they fail on purpose to demonstrate some examples that it ships with. It's annoying to have examples running along with your real tests - especially as they fail.

avatar image
0

Answer by Refzlund · Mar 09, 2019 at 04:54 PM

Hey,

To convert Unity Color (RGB) to HEX code, it has to be an Int32.

 // C#
 Color color = new Color(69, 12,  65);
 int r = (int)color.r, g = (int)color.g, b = (int)color.b;
 string _color = r.ToString("X2") + g.ToString("X2") + b.ToString("X2");

Best regards, Arthur

Comment
Add comment · 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

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

12 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

Related Questions

How do I change color with Button onClick() 0 Answers

Change color of button and convert string into int 1 Answer

Change color of a pixel to either black or white depending on how grey the pixel is 1 Answer

How to make RANDOM COLOR on shader 1 Answer

Is there any way to make a gradient within unity?,Is there any way to make a gradient? 1 Answer


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