I have hue in degrees and saturation and brightness in %.
How can I convert HSV to RGB without using EditorGUIUtility.HSVToRGB?
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 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 );
}
}