- Home /
Not exact color
Hello Everyone ,
I am trying to get a color into unity but it gives me a different color.
The color needed : RGBA : (53,140,233,255) Hex : #358CE9 The color given by Unity : RGBA : (34,90,152,255) Hex : #225A98
The code I used :
Color Blue = new Color32(53,140,233,255);
fill.color = Blue;
Can anyone please help ? Thank you in advance.
You need to give more information. What is "fill"? Where and when is that code executed? Where do you "check" the color? What's your target platform?
I just tried your code with an UI.Image component. I checked the color in the inspector and also created a screenshot and checked the color in $$anonymous$$S Paint. In each case i get exact the same color that i specified "(53, 140, 233)".
Answer by Shrikky23 · May 17, 2017 at 11:12 PM
You used Color instead of Color32. Either use the Color32 at both the places or use Color
Color = Each color component is a floating point value with a range from 0 to 1
Color32 = Each color component is a byte value with a range from 0 to 255.
Thanks for your answer , you are right about needing Color32 on both sides. However , I tried with Color32 on both sides , I still get the same results.
Color32 Blue = new Color32(53,140,233,255);
fill.color = Blue;
I tested it and it works perfectly for me. I believe two possible issues you might be facing.
fill.color might accept only Color and not Color32 Solution : Color32 blue = new Color32(53, 140, 233, 255); fill.color = (Color)blue;
fill.color is accessed by some other code, find all references in code and make sure nothing else modifies it.
Other solutions: 1. Try this in an empty project and see if you still face that problem. 2. If you do, send me a link to that empty project,
Thank you much for your help . I really appreciated it . I found the problem behind this issue . The color of the sprite which I was changing the color was not initially of white color it was of a greyish color . That's why changing it's color gave a different outcome.
That's irrelevant, Color and Color32 have implicit casting operators and convert automatically between each other.
public static implicit operator Color32(Color c)
{
return new Color32((byte)($$anonymous$$athf.Clamp01(c.r) * 255f), (byte)($$anonymous$$athf.Clamp01(c.g) * 255f), (byte)($$anonymous$$athf.Clamp01(c.b) * 255f), (byte)($$anonymous$$athf.Clamp01(c.a) * 255f));
}
public static implicit operator Color(Color32 c)
{
return new Color((float)c.r / 255f, (float)c.g / 255f, (float)c.b / 255f, (float)c.a / 255f);
}
So it doesn't matter which struct you use. We don't know what "fill" actually is, but most built-in components will use "Color". So when you specify the color as Color32 it always need to be converted. Though as i said that happens automatically.