Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public static class UtilsColor
- {
- public static void RGBToHSV(Color rgbColor, out float H, out float S, out float V)
- {
- if (rgbColor.b > rgbColor.g && rgbColor.b > rgbColor.r)
- {
- UtilsColor.RGBToHSVHelper(4f, rgbColor.b, rgbColor.r, rgbColor.g, out H, out S, out V);
- }
- else if (rgbColor.g > rgbColor.r)
- {
- UtilsColor.RGBToHSVHelper(2f, rgbColor.g, rgbColor.b, rgbColor.r, out H, out S, out V);
- }
- else
- {
- UtilsColor.RGBToHSVHelper(0f, rgbColor.r, rgbColor.g, rgbColor.b, out H, out S, out V);
- }
- }
- private static void RGBToHSVHelper(float offset, float dominantcolor, float colorone, float colortwo, out float H, out float S, out float V)
- {
- V = dominantcolor;
- if (V != 0f)
- {
- float num;
- 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;
- }
- }
- public static Color HSVToRGB(float H, float S, float V)
- {
- Color white = Color.white;
- if (S == 0f)
- {
- white.r = V;
- white.g = V;
- white.b = V;
- }
- else if (V == 0f)
- {
- white.r = 0f;
- white.g = 0f;
- white.b = 0f;
- }
- else
- {
- white.r = 0f;
- white.g = 0f;
- white.b = 0f;
- float num = H * 6f;
- int num2 = (int)Mathf.Floor(num);
- float num3 = num - (float)num2;
- float num4 = V * (1f - S);
- float num5 = V * (1f - S * num3);
- float num6 = V * (1f - S * (1f - num3));
- int num7 = num2;
- switch (num7 + 1)
- {
- case 0:
- white.r = V;
- white.g = num4;
- white.b = num5;
- break;
- case 1:
- white.r = V;
- white.g = num6;
- white.b = num4;
- break;
- case 2:
- white.r = num5;
- white.g = V;
- white.b = num4;
- break;
- case 3:
- white.r = num4;
- white.g = V;
- white.b = num6;
- break;
- case 4:
- white.r = num4;
- white.g = num5;
- white.b = V;
- break;
- case 5:
- white.r = num6;
- white.g = num4;
- white.b = V;
- break;
- case 6:
- white.r = V;
- white.g = num4;
- white.b = num5;
- break;
- case 7:
- white.r = V;
- white.g = num6;
- white.b = num4;
- break;
- }
- white.r = Mathf.Clamp(white.r, 0f, 1f);
- white.g = Mathf.Clamp(white.g, 0f, 1f);
- white.b = Mathf.Clamp(white.b, 0f, 1f);
- }
- return white;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement