FastLED 3.9.7
Loading...
Searching...
No Matches
color.cpp
1
2
3#include <Arduino.h>
4
5#include "./color.h"
6#include "./util.h"
7
8
10void Color3i::Mul(const Color3i& other_color) {
11 int r = r_;
12 int g = g_;
13 int b = b_;
14
15 r = r * int(other_color.r_) / 255;
16 g = g * int(other_color.g_) / 255;
17 b = b * int(other_color.b_) / 255;
18 Set(r, g, b);
19}
20
22void Color3i::Mulf(float scale) {
23 const int s = static_cast<int>(scale * 255.0f);
24
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;
28
29 Set(r, g, b);
30}
31
33void Color3i::Sub(const Color3i& color) {
34 if (r_ < color.r_) r_ = 0;
35 else r_ -= color.r_;
36 if (g_ < color.g_) g_ = 0;
37 else g_ -= color.g_;
38 if (b_ < color.b_) b_ = 0;
39 else b_ -= color.b_;
40}
41
43void Color3i::Add(const Color3i& color) {
44 if ((255 - r_) < color.r_) r_ = 255;
45 else r_ += color.r_;
46 if ((255 - g_) < color.g_) g_ = 255;
47 else g_ += color.g_;
48 if ((255 - b_) < color.b_) b_ = 255;
49 else b_ += color.b_;
50}
51
53uint8_t Color3i::Get(int rgb_index) const {
54 const uint8_t* rgb = At(rgb_index);
55 return rgb ? *rgb : 0;
56}
57
59void Color3i::Set(int rgb_index, uint8_t val) {
60 uint8_t* rgb = At(rgb_index);
61 if (rgb) {
62 *rgb = val;
63 }
64}
65
67void Color3i::Interpolate(const Color3i& other_color, float t) {
68 if (0.0f >= t) {
69 Set(other_color);
70 } else if (1.0f <= t) {
71 return;
72 }
73
74 Color3i new_color = other_color;
75 new_color.Mul(1.0f - t);
76 this->Mul(t);
77 this->Add(new_color);
78}
79
81uint8_t* Color3i::At(int rgb_index) {
82 switch(rgb_index) {
83 case 0: return &r_;
84 case 1: return &g_;
85 case 2: return &b_;
86 }
87 return NULL;
88}
89
91const uint8_t* Color3i::At(int rgb_index) const {
92 switch(rgb_index) {
93 case 0: return &r_;
94 case 1: return &g_;
95 case 2: return &b_;
96 }
97 return NULL;
98}
99
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));
108 v_ = max_rgb;
109
110 FloatT d = max_rgb - min_rgb;
111 s_ = max_rgb == 0 ? 0 : d / max_rgb;
112
113 if (max_rgb == min_rgb) {
114 h_ = 0; // achromatic
115 } else {
116 if (max_rgb == r) {
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;
122 }
123 h_ /= 6;
124 }
125}
126
128Color3i ColorHSV::ToRGB() const {
129 typedef double FloatT;
130 FloatT r = 0;
131 FloatT g = 0;
132 FloatT b = 0;
133
134 int i = int(h_ * 6);
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_);
139
140 switch(i % 6){
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;
147 }
148
149 return Color3i(round(r * 255), round(g * 255), round(b * 255));
150}
151
Definition color.h:8