Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by $$anonymous$$ · Oct 19, 2014 at 09:58 AM · colorintconverthex

Convert Hex (int) to Color/Color32?

Hey, I need to get better formula for converting Hex as integer to Color or Color32 form in Unity. Can someone give me a hint?

Comment
Add comment · Show 2
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 gjf · Oct 19, 2014 at 10:01 AM 0
Share

asking for a better formula implies that you've already tried something. posting that code may help to guide you...

avatar image $$anonymous$$ · Oct 19, 2014 at 10:04 AM 0
Share

Sure, but it works only with string and I want to implement it with Integers ins$$anonymous$$d

5 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by drudiverse · Oct 19, 2014 at 10:13 AM

looked for hex to rgb on google forums. unity uses rgba color. try this page... http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb

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 $$anonymous$$ · Oct 19, 2014 at 10:17 AM 0
Share

Awesome, Im using this method now Thanks!

 function hexToRgb(hex) {
     var bigint = parseInt(hex, 16);
     var r = (bigint >> 16) & 255;
     var g = (bigint >> 8) & 255;
     var b = bigint & 255;
 
     return r + "," + g + "," + b;
 }
avatar image
21

Answer by Taylor-Libonati · Feb 24, 2015 at 08:48 PM

Using a modified version of this: http://wiki.unity3d.com/index.php?title=HexConverter Here is what I have been using:

     // Note that Color32 and Color implictly convert to each other. You may pass a Color object to this method without first casting it.
     public static string colorToHex(Color32 color)
     {
         string hex = color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2");
         return hex;
     }
     
     public static Color hexToColor(string hex)
     {
         hex = hex.Replace ("0x", "");//in case the string is formatted 0xFFFFFF
         hex = hex.Replace ("#", "");//in case the string is formatted #FFFFFF
         byte a = 255;//assume fully visible unless specified in hex
         byte r = byte.Parse(hex.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
         byte g = byte.Parse(hex.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
         byte b = byte.Parse(hex.Substring(4,2), System.Globalization.NumberStyles.HexNumber);
         //Only use alpha if the string has enough characters
         if(hex.Length == 8){
             a = byte.Parse(hex.Substring(6,2), System.Globalization.NumberStyles.HexNumber);
         }
         return new Color32(r,g,b,a);
     }
Comment
Add comment · Show 5 · 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 drechter · Dec 23, 2015 at 03:44 PM 1
Share

Thank you Taylor, this helped me!

avatar image ghost_1082 · Mar 22, 2016 at 08:51 PM 2
Share

This is very similar to what I use. You may want to be careful with your Alpha value - what you want to parse starts at 6, not 4.

avatar image Snownova · Mar 22, 2016 at 08:53 PM 2
Share

Awesome function, I'd just like to point out one tiny typo:

a = byte.Parse(hex.Substring(6,2), System.Globalization.NumberStyles.HexNumber);

avatar image clamchoda · May 05, 2017 at 05:17 PM 1
Share

Threw this one in my utility class right away, thank you :)

avatar image Taylor-Libonati · Jun 05, 2017 at 04:28 PM 0
Share

Thanks ghost and Snownova for catching my mistake! I have updated the post.

avatar image
4

Answer by Dev0 · Oct 19, 2014 at 02:25 PM

You can use some bitwise operations like this:

 public Color32 ToColor(int HexVal)
 {
     byte R = (byte)((HexVal >> 16) & 0xFF);
     byte G = (byte)((HexVal >> 8) & 0xFF);
     byte B = (byte)((HexVal) & 0xFF);
     return new Color32(R, G, B, 255);
 }
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
avatar image
0

Answer by KevinMarais24B · Apr 26, 2017 at 06:10 AM

Classy extension method on Color

    public static string ToHex(this Color color)
     {
         Color32 c = color;
         var hex = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}", c.r, c.g, c.b, c.a);
         return hex;
     }
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
avatar image
0

Answer by sellverful · Mar 29 at 10:52 AM

As this is the first link that pops up, I thought I can provide much easier answer.

Unity has a built-in Utility for this:

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

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

41 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 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

Color float rgb values cannot be converted correctly to int, and hex. 1 Answer

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

Cannot convert float to int? 1 Answer

Convert a GUIText's number to an Integer value. 0 Answers

Don't know how to convert a string value to add into a int dictionnary. 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