10void Color3i::Mul(
const Color3i& other_color) {
15 r = r * int(other_color.r_) / 255;
16 g = g * int(other_color.g_) / 255;
17 b = b * int(other_color.b_) / 255;
22void Color3i::Mulf(
float scale) {
23 const int s =
static_cast<int>(scale * 255.0f);
25 int r =
static_cast<int>(r_) * s / 255;
26 int g =
static_cast<int>(g_) * s / 255;
27 int b =
static_cast<int>(b_) * s / 255;
33void Color3i::Sub(
const Color3i& color) {
34 if (r_ < color.r_) r_ = 0;
36 if (g_ < color.g_) g_ = 0;
38 if (b_ < color.b_) b_ = 0;
43void Color3i::Add(
const Color3i& color) {
44 if ((255 - r_) < color.r_) r_ = 255;
46 if ((255 - g_) < color.g_) g_ = 255;
48 if ((255 - b_) < color.b_) b_ = 255;
53uint8_t Color3i::Get(
int rgb_index)
const {
54 const uint8_t* rgb = At(rgb_index);
55 return rgb ? *rgb : 0;
59void Color3i::Set(
int rgb_index, uint8_t val) {
60 uint8_t* rgb = At(rgb_index);
67void Color3i::Interpolate(
const Color3i& other_color,
float t) {
70 }
else if (1.0f <= t) {
74 Color3i new_color = other_color;
75 new_color.Mul(1.0f - t);
81uint8_t* Color3i::At(
int rgb_index) {
91const uint8_t* Color3i::At(
int rgb_index)
const {
101void ColorHSV::FromRGB(
const Color3i& rgb) {
102 typedef double FloatT;
103 FloatT r = (FloatT) rgb.r_/255.f;
104 FloatT g = (FloatT) rgb.g_/255.f;
105 FloatT b = (FloatT) rgb.b_/255.f;
106 FloatT max_rgb = max(r, max(g, b));
107 FloatT min_rgb = min(r, min(g, b));
110 FloatT d = max_rgb - min_rgb;
111 s_ = max_rgb == 0 ? 0 : d / max_rgb;
113 if (max_rgb == min_rgb) {
117 h_ = (g - b) / d + (g < b ? 6 : 0);
118 }
else if (max_rgb == g) {
119 h_ = (b - r) / d + 2;
120 }
else if (max_rgb == b) {
121 h_ = (r - g) / d + 4;
128Color3i ColorHSV::ToRGB()
const {
129 typedef double FloatT;
135 FloatT f = h_ * 6.0 -
static_cast<FloatT
>(i);
136 FloatT p = v_ * (1.0 - s_);
137 FloatT q = v_ * (1.0 - f * s_);
138 FloatT t = v_ * (1.0 - (1.0 - f) * s_);
141 case 0: r = v_, g = t, b = p;
break;
142 case 1: r = q, g = v_, b = p;
break;
143 case 2: r = p, g = v_, b = t;
break;
144 case 3: r = p, g = q, b = v_;
break;
145 case 4: r = t, g = p, b = v_;
break;
146 case 5: r = v_, g = p, b = q;
break;
149 return Color3i(round(r * 255), round(g * 255), round(b * 255));