FastLED 3.9.15
Loading...
Searching...
No Matches
color.cpp
Go to the documentation of this file.
1
2
3#include <Arduino.h>
4
5#include "./color.h"
6#include "./util.h"
7#include "fl/math_macros.h"
8
9
11void Color3i::Mul(const Color3i& other_color) {
12 int r = r_;
13 int g = g_;
14 int b = b_;
15
16 r = r * int(other_color.r_) / 255;
17 g = g * int(other_color.g_) / 255;
18 b = b * int(other_color.b_) / 255;
19 Set(r, g, b);
20}
21
23void Color3i::Mulf(float scale) {
24 const int s = static_cast<int>(scale * 255.0f);
25
26 int r = static_cast<int>(r_) * s / 255;
27 int g = static_cast<int>(g_) * s / 255;
28 int b = static_cast<int>(b_) * s / 255;
29
30 Set(r, g, b);
31}
32
34void Color3i::Sub(const Color3i& color) {
35 if (r_ < color.r_) r_ = 0;
36 else r_ -= color.r_;
37 if (g_ < color.g_) g_ = 0;
38 else g_ -= color.g_;
39 if (b_ < color.b_) b_ = 0;
40 else b_ -= color.b_;
41}
42
44void Color3i::Add(const Color3i& color) {
45 if ((255 - r_) < color.r_) r_ = 255;
46 else r_ += color.r_;
47 if ((255 - g_) < color.g_) g_ = 255;
48 else g_ += color.g_;
49 if ((255 - b_) < color.b_) b_ = 255;
50 else b_ += color.b_;
51}
52
54uint8_t Color3i::Get(int rgb_index) const {
55 const uint8_t* rgb = At(rgb_index);
56 return rgb ? *rgb : 0;
57}
58
60void Color3i::Set(int rgb_index, uint8_t val) {
61 uint8_t* rgb = At(rgb_index);
62 if (rgb) {
63 *rgb = val;
64 }
65}
66
68void Color3i::Interpolate(const Color3i& other_color, float t) {
69 if (0.0f >= t) {
70 Set(other_color);
71 } else if (1.0f <= t) {
72 return;
73 }
74
75 Color3i new_color = other_color;
76 new_color.Mul(1.0f - t);
77 this->Mul(t);
78 this->Add(new_color);
79}
80
82uint8_t* Color3i::At(int rgb_index) {
83 switch(rgb_index) {
84 case 0: return &r_;
85 case 1: return &g_;
86 case 2: return &b_;
87 }
88 return NULL;
89}
90
92const uint8_t* Color3i::At(int rgb_index) const {
93 switch(rgb_index) {
94 case 0: return &r_;
95 case 1: return &g_;
96 case 2: return &b_;
97 }
98 return NULL;
99}
100
102void ColorHSV::FromRGB(const Color3i& rgb) {
103 typedef double FloatT;
104 FloatT r = (FloatT) rgb.r_/255.f;
105 FloatT g = (FloatT) rgb.g_/255.f;
106 FloatT b = (FloatT) rgb.b_/255.f;
107 FloatT max_rgb = MAX(r, MAX(g, b));
108 FloatT min_rgb = MIN(r, MIN(g, b));
109 v_ = max_rgb;
110
111 FloatT d = max_rgb - min_rgb;
112 s_ = max_rgb == 0 ? 0 : d / max_rgb;
113
114 if (max_rgb == min_rgb) {
115 h_ = 0; // achromatic
116 } else {
117 if (max_rgb == r) {
118 h_ = (g - b) / d + (g < b ? 6 : 0);
119 } else if (max_rgb == g) {
120 h_ = (b - r) / d + 2;
121 } else if (max_rgb == b) {
122 h_ = (r - g) / d + 4;
123 }
124 h_ /= 6;
125 }
126}
127
130 typedef double FloatT;
131 FloatT r = 0;
132 FloatT g = 0;
133 FloatT b = 0;
134
135 int i = int(h_ * 6);
136 FloatT f = h_ * 6.0 - static_cast<FloatT>(i);
137 FloatT p = v_ * (1.0 - s_);
138 FloatT q = v_ * (1.0 - f * s_);
139 FloatT t = v_ * (1.0 - (1.0 - f) * s_);
140
141 switch(i % 6){
142 case 0: r = v_, g = t, b = p; break;
143 case 1: r = q, g = v_, b = p; break;
144 case 2: r = p, g = v_, b = t; break;
145 case 3: r = p, g = q, b = v_; break;
146 case 4: r = t, g = p, b = v_; break;
147 case 5: r = v_, g = p, b = q; break;
148 }
149
150 return Color3i(round(r * 255), round(g * 255), round(b * 255));
151}
uint16_t scale
Definition Noise.ino:74
static uint32_t t
Definition Luminova.h:54
#define MIN(a, b)
Definition math_macros.h:41
#define MAX(a, b)
Definition math_macros.h:37
void Mulf(float scale)
Definition color.cpp:23
uint8_t b_
Definition color.h:56
void Add(const Color3i &color)
Definition color.cpp:44
Color3i(uint8_t r, uint8_t g, uint8_t b)
Definition color.h:15
void Mul(const Color3i &other_color)
Definition color.cpp:11
void Interpolate(const Color3i &other_color, float t)
Definition color.cpp:68
void Sub(const Color3i &color)
Definition color.cpp:34
void Set(uint8_t r, uint8_t g, uint8_t b)
Definition color.h:25
uint8_t g_
Definition color.h:56
uint8_t * At(int rgb_index)
Definition color.cpp:82
uint8_t r_
Definition color.h:56
uint8_t Get(int rgb_index) const
Definition color.cpp:54
Definition color.h:6
float v_
Definition color.h:108
float h_
Definition color.h:108
Color3i ToRGB() const
Definition color.cpp:129
void FromRGB(const Color3i &rgb)
Definition color.cpp:102
float s_
Definition color.h:108
#define round(x)
Definition util.h:11