Blame toonz/sources/stdfx/hsvutil.cpp
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "trop.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "hsvutil.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void OLDRGB2HSV(double r, double g, double b, double *h, double *s, double *v) {
|
|
Shinya Kitaoka |
120a6e |
double max, min;
|
|
Shinya Kitaoka |
120a6e |
double delta;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
max = std::max({r, g, b});
|
|
Shinya Kitaoka |
120a6e |
min = std::min({r, g, b});
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
*v = max;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if (max != 0)
|
|
Shinya Kitaoka |
120a6e |
*s = (max - min) / max;
|
|
Shinya Kitaoka |
120a6e |
else
|
|
Shinya Kitaoka |
120a6e |
*s = 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if (*s == 0)
|
|
Shinya Kitaoka |
120a6e |
*h = 0;
|
|
Shinya Kitaoka |
120a6e |
else {
|
|
Shinya Kitaoka |
120a6e |
delta = max - min;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
if (r == max)
|
|
Shinya Kitaoka |
120a6e |
*h = (g - b) / delta;
|
|
Shinya Kitaoka |
120a6e |
else if (g == max)
|
|
Shinya Kitaoka |
120a6e |
*h = 2 + (b - r) / delta;
|
|
Shinya Kitaoka |
120a6e |
else if (b == max)
|
|
Shinya Kitaoka |
120a6e |
*h = 4 + (r - g) / delta;
|
|
Shinya Kitaoka |
120a6e |
*h = *h * 60;
|
|
Shinya Kitaoka |
120a6e |
if (*h < 0) *h += 360;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void OLDHSV2RGB(double hue, double sat, double value, double *red,
|
|
Shinya Kitaoka |
120a6e |
double *green, double *blue) {
|
|
Shinya Kitaoka |
120a6e |
int i;
|
|
Shinya Kitaoka |
120a6e |
double p, q, t, f;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
// if (hue > 360 || hue < 0)
|
|
Shinya Kitaoka |
120a6e |
// hue=0;
|
|
Shinya Kitaoka |
120a6e |
if (hue > 360) hue -= ((int)hue / 360) * 360;
|
|
Shinya Kitaoka |
120a6e |
// hue-=360;
|
|
Shinya Kitaoka |
120a6e |
if (hue < 0) hue += (1 - (int)hue / 360) * 360;
|
|
Shinya Kitaoka |
120a6e |
// hue+=360;
|
|
Shinya Kitaoka |
120a6e |
if (sat < 0) sat = 0;
|
|
Shinya Kitaoka |
120a6e |
if (sat > 1) sat = 1;
|
|
Shinya Kitaoka |
120a6e |
if (value < 0) value = 0;
|
|
Shinya Kitaoka |
120a6e |
if (value > 1) value = 1;
|
|
Shinya Kitaoka |
120a6e |
if (sat == 0) {
|
|
Shinya Kitaoka |
120a6e |
*red = value;
|
|
Shinya Kitaoka |
120a6e |
*green = value;
|
|
Shinya Kitaoka |
120a6e |
*blue = value;
|
|
Shinya Kitaoka |
120a6e |
} else {
|
|
Shinya Kitaoka |
120a6e |
if (hue == 360) hue = 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
hue = hue / 60;
|
|
Shinya Kitaoka |
120a6e |
i = (int)hue;
|
|
Shinya Kitaoka |
120a6e |
f = hue - i;
|
|
Shinya Kitaoka |
120a6e |
p = value * (1 - sat);
|
|
Shinya Kitaoka |
120a6e |
q = value * (1 - (sat * f));
|
|
Shinya Kitaoka |
120a6e |
t = value * (1 - (sat * (1 - f)));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
switch (i) {
|
|
Shinya Kitaoka |
120a6e |
case 0:
|
|
Shinya Kitaoka |
120a6e |
*red = value;
|
|
Shinya Kitaoka |
120a6e |
*green = t;
|
|
Shinya Kitaoka |
120a6e |
*blue = p;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case 1:
|
|
Shinya Kitaoka |
120a6e |
*red = q;
|
|
Shinya Kitaoka |
120a6e |
*green = value;
|
|
Shinya Kitaoka |
120a6e |
*blue = p;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case 2:
|
|
Shinya Kitaoka |
120a6e |
*red = p;
|
|
Shinya Kitaoka |
120a6e |
*green = value;
|
|
Shinya Kitaoka |
120a6e |
*blue = t;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case 3:
|
|
Shinya Kitaoka |
120a6e |
*red = p;
|
|
Shinya Kitaoka |
120a6e |
*green = q;
|
|
Shinya Kitaoka |
120a6e |
*blue = value;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case 4:
|
|
Shinya Kitaoka |
120a6e |
*red = t;
|
|
Shinya Kitaoka |
120a6e |
*green = p;
|
|
Shinya Kitaoka |
120a6e |
*blue = value;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
case 5:
|
|
Shinya Kitaoka |
120a6e |
*red = value;
|
|
Shinya Kitaoka |
120a6e |
*green = p;
|
|
Shinya Kitaoka |
120a6e |
*blue = q;
|
|
Shinya Kitaoka |
120a6e |
break;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|