Skip to content

Instantly share code, notes, and snippets.

@dvdfu
Created July 21, 2020 16:13
Show Gist options
  • Save dvdfu/629a73bdcfbada7b36d2b64f7f96b398 to your computer and use it in GitHub Desktop.
Save dvdfu/629a73bdcfbada7b36d2b64f7f96b398 to your computer and use it in GitHub Desktop.
Interpolate between two HSV colors (Unity)
public static Color LerpHSV(Color a, Color b, float x) {
Vector3 ah = RGB2HSV(a);
Vector3 bh = RGB2HSV(b);
return new Vector3(
Mathf.LerpAngle(ah.x, bh.x, x),
Mathf.Lerp(ah.y, bh.y, x),
Mathf.Lerp(ah.z, bh.z, x)
);
}
static Vector3 RGB2HSV(Color color) {
float cmax = MathUtils.Max3(color.r, color.g, color.b);
float cmin = MathUtils.Min3(color.r, color.g, color.b);
float delta = cmax - cmin;
float hue = 0;
float saturation = 0;
if (cmax == color.r) {
hue = 60 * (((color.g - color.b) / delta) % 6);
} else if (cmax == color.g) {
hue = 60 * ((color.b - color.r) / delta + 2);
} else if (cmax == color.b) {
hue = 60 * ((color.r - color.g) / delta + 4);
}
if (cmax > 0) {
saturation = delta / cmax;
}
return new Vector3(hue, saturation, cmax);
}
static Color HSV2RGB(Vector3 color) {
float hue = color.x;
float c = color.z * color.y;
float x = c * (1 - Mathf.Abs((hue / 60) % 2 - 1));
float m = color.z - c;
float r = 0;
float g = 0;
float b = 0;
if (hue < 60) {
r = c;
g = x;
} else if (hue < 120) {
r = x;
g = c;
} else if (hue < 180) {
g = c;
b = x;
} else if (hue < 240) {
g = x;
b = c;
} else if (hue < 300) {
r = x;
b = c;
} else {
r = c;
b = x;
}
return new Color(r + m, g + m, b + m);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment