12 const std::string Color::TypeNames[] = {
"rgb",
"hsv",
"hsvcone",
"unknown" };
15 const appear_t RGBColor::MAX_DIST = 1.73205180756888;
17 RGBColor::RGBColor(
const Color&c)
22 if ( (rgbc =
dynamic_cast<const RGBColor*
>(&c)) != NULL) c_ = rgbc->c_;
23 else if ( (hsvc =
dynamic_cast<const HSVColor*
>(&c)) != NULL)
25 appear_t h = hsvc->H() * 180 / M_PI, s = hsvc->S(), v = hsvc->V();
26 NUKLEI_RANGE_CHECK(h, 0, 360);
38 i = (int)std::floor( hTemp );
41 q = v * ( 1 - s * f );
42 t = v * ( 1 - s * ( 1 - f ) );
46 case 0:{R() = v;G() = t;B() = p;
break;}
47 case 1:{R() = q;G() = v;B() = p;
break;}
48 case 2:{R() = p;G() = v;B() = t;
break;}
49 case 3:{R() = p;G() = q;B() = v;
break;}
50 case 4:{R() = t;G() = p;B() = v;
break;}
51 default:{R() = v;G() = p;B() = q;
break;}
54 else if ( (hsvcc =
dynamic_cast<const HSVConeColor*
>(&c)) != NULL)
64 void RGBColor::makeRandom()
66 for (
int i = 0; i < 3; ++i)
72 const appear_t HSVColor::MAX_DIST = 2.000001;
74 HSVColor::HSVColor(
const Color& c)
79 if ( (rgbc =
dynamic_cast<const RGBColor*
>(&c)) != NULL)
82 appear_t r = rgbc->R(), g = rgbc->G(), b = rgbc->B();
84 rgbMin = std::min(r,std::min(g,b));
85 rgbMax = std::max(r,std::max(g,b));
86 delta = rgbMax - rgbMin;
99 if (rgbMax == r && rgbMax != g)
100 H() += (g - b) / delta;
101 if (rgbMax == g && rgbMax != b)
102 H() += (2 + (b - r) / delta);
103 if (rgbMax == b && rgbMax != r)
104 H() += (4 + (r - g) / delta);
112 else if ( (hsvc =
dynamic_cast<const HSVColor*
>(&c)) != NULL) c_ = hsvc->c_;
113 else if ( (hsvcc =
dynamic_cast<const HSVConeColor*
>(&c)) != NULL)
116 V() = hsvcc->WV() / hsvcc->W();
119 S() = std::sqrt( std::pow(hsvcc->SCosH(), 2) + std::pow(hsvcc->SSinH(), 2) ) / V();
120 if (hsvcc->SSinH() >= 0) H() = ACos(hsvcc->SCosH() / (V()*S()));
121 else H() = 2*M_PI-ACos(hsvcc->SCosH() / (V()*S()));
135 void HSVColor::makeRandom()
144 if (v.Y() >= 0) c_ = Vector3(ACos(la::normalized(v).Dot(Vector3::UNIT_X)),
147 else c_ = Vector3(2*M_PI-ACos(la::normalized(v).Dot(Vector3::UNIT_X)),
157 return distanceTo(
dynamic_cast<const HSVColor&
>(c));
163 return (c_-hsv.c_).Length();
168 const appear_t HSVConeColor::MAX_DIST = 2.000001;
170 HSVConeColor::HSVConeColor(
const Color& c)
175 if ( (rgbc =
dynamic_cast<const RGBColor*
>(&c)) != NULL)
180 valueWeight_ = hsvCone.valueWeight_;
182 else if ( (hsvc =
dynamic_cast<const HSVColor*
>(&c)) != NULL)
185 ec.X() = std::cos(hsvc->H()) * hsvc->S() * hsvc->V();
186 ec.Y() = std::sin(hsvc->H()) * hsvc->S() * hsvc->V();
187 NUKLEI_RANGE_CHECK(HSV_METRIC_VALUE_WEIGHT, 0, 1);
188 ec.Z() = hsvc->V()*HSV_METRIC_VALUE_WEIGHT;
190 valueWeight_ = HSV_METRIC_VALUE_WEIGHT;
192 else if ( (hsvcc =
dynamic_cast<const HSVConeColor*
>(&c)) != NULL)
195 valueWeight_ = hsvcc->valueWeight_;
202 void HSVConeColor::makeRandom()
214 valueWeight_ = HSV_METRIC_VALUE_WEIGHT;
221 return distanceTo(
dynamic_cast<const HSVConeColor&
>(c));
226 return (c_-hsvc.c_).Length();