public Color toColor(HSVA c) { if (approxEq(s, 0)) { return(new Color(this.v, this.v, this.v, a)); // -> greyscale } var sixth = h / 60; int i = Mathf.FloorToInt(sixth); // [i]nteger part float r = sixth - i; // [r]emainder part var v = this.v; var p = v * (1 - s); var q = v * (1 - s * r); var t = v * (1 - s * (1 - r)); var color = new Color(0, 0, 0, a); switch (i) { case 0: color.r = v; color.g = t; color.b = p; break; case 1: color.r = q; color.g = v; color.b = p; break; case 2: color.r = p; color.g = v; color.b = t; break; case 3: color.r = p; color.g = q; color.b = v; break; case 4: color.r = t; color.g = p; color.b = v; break; default: color.r = v; color.g = p; color.b = q; break; } return(color); }
public static HSVA Grad(HSVA src, HSVA tar, float t) { t = Mathf.Clamp01(t); var steps = 100; var colors = 2; var parts = colors - 1; var gradient = new List <HSVA>(); var gradientIndex = 0; var partSteps = Mathf.Floor(steps / parts); var remainder = steps - (partSteps * parts); for (var col = 0; col < parts; col++) { var c1 = src; var c2 = tar; var goCW = (src.h >= tar.h) ? (src.h - tar.h) : (1 + src.h - tar.h); var goCCW = (src.h >= tar.h) ? (1 + tar.h - src.h) : (tar.h - src.h); if (col == parts - 1) { partSteps += remainder; } for (var step = 0; step < steps; step++) { var p = step / steps; var h = (goCW <= goCCW) ? src.h + (goCW * p) : src.h - (goCCW * p); if (h < 0) { h = 1 + h; } if (h > 1) { h = h - 1; } var s = ((1 - p) * src.s) + (p * tar.s); var v = ((1 - p) * src.v) + (p * tar.v); gradient[gradientIndex] = new HSVA(h, s, v, 1); gradientIndex++; } } return(new HSVA()); }
public static HSVA Lerp(HSVA src, HSVA tar, float t) { t = Mathf.Clamp01(t); float h, s; if (approxEq(src.v, 0)) { h = tar.h; s = tar.s; } else if (approxEq(tar.v, 0)) { h = src.h; s = src.s; } else { if (approxEq(tar.s, 0)) { h = tar.h; } else if (approxEq(src.s, 0)) { h = src.h; } else { var angle = Mathf.LerpAngle(src.h * 360f, tar.h * 360f, t); while (angle < 0f) { angle += 360f; } while (angle > 360f) { angle -= 360f; } h = angle / 360f; } s = Mathf.Lerp(src.s, tar.s, t); } return(new HSVA(h, s, Mathf.Lerp(src.v, tar.v, t), Mathf.Lerp(src.a, tar.a, t))); }
public Color32 toColor32(HSVA c) { return((Color32)toColor(c)); }