- Home /
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?
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;
}
}
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;
}
}
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
Yes, you're right ^^ Thanks for the hint, i fixed it.
Answer by Eric5h5 · May 06, 2014 at 07:01 PM
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
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;
}
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);
}
}
}
}
}
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 );
}
}
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
Follow this Question
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