void RGB_HSV(int r, int g, int b, float* h, float* s, float* v) { int vmin = min(r, g); if (b < vmin) vmin = b; int vmax = max(r, g); if (b > vmax) vmax = b; int delta = vmax - vmin; *v = (float)vmax / 255.0f; if (delta == 0) { *h = 0; *s = 0; } else { *s = (float)delta / (float)vmax; if (r == vmax && g >= b) *h = (1.0f / 6.0f) * (float)(g - b) / (float)delta; else if (r == vmax) *h = ((1.0f / 6.0f) * (float)(g - b) / (float)delta) + 1.0f; else if (g == vmax) *h = ((1.0f / 6.0f) * (float)(b - r) / (float)delta) + (1.0f / 3.0f); else if (b == vmax) *h = ((1.0f / 6.0f) * (float)(r - g) / (float)delta) + (2.0f / 3.0f); if (*h < 0.0f) *h += 1.0f; else if (*h > 1.0f) *h -= 1.0f; } } void HSV_RGB(float h, float s, float v, int* r, int* g, int* b) { v *= 255.0f; int vi = (int)v; if (s == 0.0f) { *r = vi; *g = vi; *b = vi; } else { h *= 6.0f; int hi = (int)h % 6; float f = h - (float)hi; int a = (int)(v * (1.0f - s)); int b = (int)(v * (1.0f - (f * s))); int c = (int)(v * (1.0f - ((1.0f - f) * s))); switch (hi) { case 0: *r = vi; *g = c; *b = a; break; case 1: *r = b; *g = vi;
} }
}
*b = a; break; case 2: *r = a; *g = vi; *b = c; break; case 3: *r = a; *g = b; *b = vi; break; case 4: *r = c; *g = a; *b = vi; break; default: *r = vi; *g = a; *b = b; break;