public static Hsv ToHsv(this Rgb rgb) { var r_ = rgb.R / 255.0f; var g_ = rgb.G / 255.0f; var b_ = rgb.B / 255.0f; var cMax = Math.Max(r_, Math.Max(g_, b_)); var cMin = Math.Min(r_, Math.Min(g_, b_)); var d = cMax - cMin; var hue = Hue(d, r_, g_, b_, cMax); var saturation = cMax == 0.0f ? 0.0f : d / cMax; var value = cMax; return(new Hsv(hue, saturation, value)); }
private static Rgb HsvToRgb(float h, float s, float v) { int hi = (int)Math.Floor(h / 60.0) % 6; float f = (h / 60.0f) - (float)Math.Floor(h / 60.0); float p = v * (1.0f - s); float q = v * (1.0f - (f * s)); float t = v * (1.0f - ((1.0f - f) * s)); Rgb ret; switch (hi) { case 0: ret = GetRgb(v, t, p); break; case 1: ret = GetRgb(q, v, p); break; case 2: ret = GetRgb(p, v, t); break; case 3: ret = GetRgb(p, q, v); break; case 4: ret = GetRgb(t, p, v); break; case 5: ret = GetRgb(v, p, q); break; default: ret = new Rgb(0x00, 0x00, 0x00); break; } return(ret); }