dl
dl_color.h
浏览该文件的文档.
1
14#pragma once
15
16#include "math/dl_random.h"
17#include "math/dl_math.h"
18#include "base/dl_range_every.h"
19#include "dl_type.h"
20
21namespace dl
22{
23
24using Color3F = std::array<float, 3>;
25
30inline Color createColorRandom(float a = 1.0f)
31{
32 Color ret;
33 ret[0] = Random::Float(0_f, 1_f);
34 ret[1] = Random::Float(0_f, 1_f);
35 ret[2] = Random::Float(0_f, 1_f);
36 ret[3] = a;
37
38 return ret;
39}
40
46constexpr Color createColor(const Color& col, float a = 1.0f)
47{
48 return { col[0], col[1], col[2], a };
49}
50constexpr Color createColor(const Position3& col, float a = 1.0f)
51{
52 return { col[0], col[1], col[2], a };
53}
54
60constexpr Color createColor(const Color3F& rgb, float a = 1.0f)
61{
62 return { rgb[0], rgb[1], rgb[2], a };
63}
64
69constexpr Color createColor(uint32_t dw_rgba)
70{
71 //RGBA -> rgba
72 return {
73 (dw_rgba >> 24 & 255) / 255.0f,
74 (dw_rgba >> 16 & 255) / 255.0f,
75 (dw_rgba >> 8 & 255) / 255.0f,
76 (dw_rgba >> 0 & 255) / 255.0f };
77}
78
82constexpr Color createColor(uint8_t R, uint8_t G, uint8_t B, uint8_t A)
83{
84 return { R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f };
85}
86
90constexpr uint32_t AssembleColor(std::array<uint8_t, 4> arr_col)
91{
92 return ((uint32_t)arr_col[0] << 24)
93 | ((uint32_t)arr_col[1] << 16)
94 | ((uint32_t)arr_col[2] << 8)
95 | ((uint32_t)arr_col[3] << 0);
96}
97
101constexpr std::array<uint8_t, 4> GetColorChannel(uint32_t dw_col)
102{
103 return {
104 (uint8_t)(dw_col >> 24),
105 (uint8_t)(dw_col >> 16 & 255),
106 (uint8_t)(dw_col >> 8 & 255),
107 (uint8_t)(dw_col >> 0 & 255) };
108}
109
113constexpr void BlendColorAlpha(Color& bg, const Color& fg)
114{
115 // 公式:
116 // 输出颜色 = 前景色 × 前景透明度 + 背景色 ×(1 - 前景透明度)
117 bg.r = fg.r * fg.a + bg.r * (1.0f - fg.a);
118 bg.g = fg.g * fg.a + bg.g * (1.0f - fg.a);
119 bg.b = fg.b * fg.a + bg.b * (1.0f - fg.a);
120 bg.a = fg.a + bg.a * (1.0f - fg.a);
121}
122
126inline void colorSaturate(Color& col)
127{
128 Every::Saturate(col);
129}
130
134inline void colorInverse(Color& col)
135{
136 col = { 1.0f - col[0], 1.0f - col[1], 1.0f - col[2], col[3] };
137}
138
139[[nodiscard]]
140inline Color colorInverse(const Color& col)
141{
142 Color ret = col;
143 colorInverse(ret);
144 return ret;
145}
146
150inline bool isColorOpaque(const Color& col)
151{
152 return col[3] == 1.0f;
153}
154
159constexpr std::array<uint8_t, 4> getColorChannel(uint32_t dw_col)
160{
161 return {
162 (uint8_t)(dw_col >> 24 & 255),
163 (uint8_t)(dw_col >> 16 & 255),
164 (uint8_t)(dw_col >> 8 & 255),
165 (uint8_t)(dw_col >> 0 & 255) };
166}
167
168
172inline Color colorLerp(float cur)
173{
174 cur *= 6.0f;
175 Color col;
176 if (cur < 1.0f)
177 col = { 1.0f, 0.0f, cur, 1.0f };
178 else if (cur < 2.0f)
179 col = { 2.0f - cur, 0.0f, 1.0f, 1.0f };
180 else if (cur < 3.0f)
181 col = { 0.0f, cur - 2.0f, 1.0f, 1.0f };
182 else if (cur < 4.0f)
183 col = { 0.0f, 1.0f, 4.0f - cur, 1.0f };
184 else if (cur < 5.0f)
185 col = { cur - 4.0f, 1.0f, 0.0f, 1.0f };
186 else
187 col = { 1.0f, 6.0f - cur, 0.0f, 1.0f };
188 return col;
189}
190
198inline Color createColorHSV(float h, float s, float v, float a = 1.0f)
199{
200 float r, g, b;
201
202 int i = static_cast<int>(h / 60.0f) % 6;
203 float f = h / 60.0f - i;
204 float p = v * (1.0f - s);
205 float q = v * (1.0f - f * s);
206 float t = v * (1.0f - (1.0f - f) * s);
207
208 switch (i)
209 {
210 case 0:
211 r = v; g = t; b = p;
212 break;
213 case 1:
214 r = q; g = v; b = p;
215 break;
216 case 2:
217 r = p; g = v; b = t;
218 break;
219 case 3:
220 r = p; g = q; b = v;
221 break;
222 case 4:
223 r = t; g = p; b = v;
224 break;
225 default:
226 r = v; g = p; b = q;
227 break;
228 }
229
230 return { r, g, b, a };
231}
232
233inline Color operator""_c(unsigned long long x)
234{
235 return createColor((uint32_t)x);
236}
237
239namespace RGBA
240{
244 constexpr uint32_t Create(const Color& col)
245 {
246 return (uint32_t(col[0] * 255) << 24)//r
247 | (uint32_t(col[1] * 255) << 16)//g
248 | (uint32_t(col[2] * 255) << 8)//b
249 | (uint32_t(col[3] * 255) << 0);//a
250 }
251
255 constexpr uint32_t Create(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
256 {
257 return (r << 24) | (g << 16) | (b << 8) | (a << 0);
258 }
259
263 constexpr uint32_t GetA(uint32_t dw_col)
264 {
265 return dw_col & 0x000000ff;
266 }
267
271 inline uint32_t ToABGR(uint32_t dw_col)
272 {
273 return Math::ByteReverse(dw_col);
274 }
275}
276
277
279namespace ABGR
280{
284constexpr uint32_t Create(const Color& col)
285{
286 return (uint32_t(col[0] * 255) << 0)//r
287 | (uint32_t(col[1] * 255) << 8)//g
288 | (uint32_t(col[2] * 255) << 16)//b
289 | (uint32_t(col[3] * 255) << 24);//a
290}
291
296constexpr Color CreateColor(uint32_t dw)
297{
298 // ABGR -> rgba
299 return {
300 (dw >> 0 & 255) / 255.0f,
301 (dw >> 8 & 255) / 255.0f,
302 (dw >> 16 & 255) / 255.0f,
303 (dw >> 24 & 255) / 255.0f };
304}
305
309constexpr uint8_t GetA(uint32_t dw_col)
310{
311 return (dw_col & 0xff000000) >> 24;
312}
313}
314
316namespace BGRA
317{
318
322constexpr uint32_t ToABGR(uint32_t dw_col)
323{
324 return ((dw_col & 0x000000ff) << 24)
325 | (dw_col >> 8);
326}
327
328}
329
331namespace ARGB
332{
333
337constexpr uint32_t ToABGR(uint32_t dw_col)
338{
339 return (dw_col & 0xff00ff00)
340 | ((dw_col & 0x00ff0000) >> 16)
341 | ((dw_col & 0x000000ff) << 16);
342}
343
344}
345
346namespace ColorDef
347{
348 constexpr Color NONE = { 0.0f, 0.0f, 0.0f, 0.0f };
349
350 constexpr Color WHITE = { 1.0f, 1.0f, 1.0f, 1.0f };
351 constexpr Color BLACK = { 0.0f, 0.0f, 0.0f, 1.0f };
352 constexpr Color RED = { 1.0f, 0.0f, 0.0f, 1.0f };
353 constexpr Color GREEN = { 0.0f, 1.0f, 0.0f, 1.0f };
354 constexpr Color BLUE = { 0.0f, 0.0f, 1.0f, 1.0f };
355 constexpr Color YELLOW = { 1.0f, 1.0f, 0.0f, 1.0f };
356 constexpr Color PURPLE = { 1.0f, 0.0f, 1.0f, 1.0f };
357 constexpr Color CYAN = { 0.0f, 1.0f, 1.0f, 1.0f };
358
359 constexpr Color ORANGE = { 1.0f, 0.5f, 0.0f, 1.0f };
360
361 constexpr Color GREEN_DARK = { 0.0f, 100.0f / 255.0f, 0.0f, 1.0f };
362
363
364
365 constexpr Color DISABLE = { 0.298f, 0.298f, 0.298f, 1.0f };
366 constexpr Color TEXT_BG = { 0.0f, 0.0f, 0.0f, 0.5f };
367
368 constexpr Color FACTOR_GRAY = { 0.299f, 0.587f, 0.114f, 1.0f };
369
370 constexpr Color GRAY = { 0.5f, 0.5f, 0.5f, 1.0f };
371 constexpr Color GRAY_LIGHT = { 0.75f, 0.75f, 0.75f, 1.0f };
372 constexpr Color GRAY_DARK = { 0.25f, 0.25f, 0.25f, 1.0f };
373
374 constexpr Color CLEAR_SCREEN = { 0.0f, 0.2f, 0.4f, 1.0f };
375
376 //https://www.color.org/chardata/rgb/BT709.xalter
377 constexpr Color AXIS_X = { 0.64f, 0.30f, 0.15f, 1.0f };
378 constexpr Color AXIS_Y = { 0.33f, 0.60f, 0.06f, 1.0f };
379 constexpr Color AXIS_Z = { 0.03f, 0.10f, 0.79f, 1.0f };
380};
381
387inline Color createColor(std::string_view str)
388{
389 try
390 {
391 if(str.empty())
392 return ColorDef::WHITE;
393 uint32_t dw = (uint32_t)std::stoul(std::string{ str }, nullptr, 16);
394 return createColor(dw);
395 }
396 catch (...)
397 {
398 }
399 return ColorDef::WHITE;
400}
401
402
403}
static T Float(T min, T max)
返回[min,max)区间的 浮点数随机值
数学相关的通用算法
随机数
范围操作
通用类型
ABGR专用(请优先使用RGBA,除非你知道你在做什么)
constexpr uint32_t Create(const Color &col)
颜色转为uint32_t颜色
constexpr uint8_t GetA(uint32_t dw_col)
ABGR颜色获取A通道
constexpr Color CreateColor(uint32_t dw)
创建颜色
ARGB专用(请优先使用RGBA,除非你知道你在做什么)
constexpr uint32_t ToABGR(uint32_t dw_col)
转为ABGR颜色
BGRA专用(请优先使用RGBA,除非你知道你在做什么)
constexpr uint32_t ToABGR(uint32_t dw_col)
转为ABGR颜色
颜色预定义
constexpr Color NONE
constexpr Color CYAN
constexpr Color RED
constexpr Color ORANGE
constexpr Color AXIS_Y
constexpr Color BLUE
constexpr Color FACTOR_GRAY
转灰色因子(并不能转成真的灰色,可使用 Sprite::Blend::GRAY)
constexpr Color GRAY_LIGHT
constexpr Color WHITE
constexpr Color GRAY_DARK
constexpr Color DISABLE
constexpr Color BLACK
constexpr Color YELLOW
constexpr Color GREEN
constexpr Color PURPLE
constexpr Color GRAY
constexpr Color GREEN_DARK
constexpr Color TEXT_BG
文本背景,半透明黑色
constexpr Color AXIS_Z
constexpr Color CLEAR_SCREEN
清屏颜色
constexpr Color AXIS_X
void Saturate(C &c)
取[0, 1]
uint32_t ByteReverse(uint32_t x)
翻转字节序
RGBA专用
uint32_t ToABGR(uint32_t dw_col)
转为ABGR颜色
constexpr uint32_t GetA(uint32_t dw_col)
uint32_t颜色获取A通道
constexpr uint32_t Create(const Color &col)
颜色转为uint32_t颜色
Color createColorRandom(float a=1.0f)
创建随机颜色
Color createColorHSV(float h, float s, float v, float a=1.0f)
从HSV颜色模型创建颜色
Color colorLerp(float cur)
颜色插值
std::array< float, 3 > Color3F
constexpr std::array< uint8_t, 4 > GetColorChannel(uint32_t dw_col)
获取颜色分量,从左到右
constexpr uint32_t AssembleColor(std::array< uint8_t, 4 > arr_col)
通道数组转为 转为uint32_t颜色 例如 00, 11, 22, 33 -> 0x00112233
Array< Float, 3 > Position3
Array< float, 4 > Color
constexpr Color createColor(const Color &col, float a=1.0f)
创建指定透明度颜色
constexpr void BlendColorAlpha(Color &bg, const Color &fg)
alpha混合
bool isColorOpaque(const Color &col)
不透明(a通道为1)
constexpr std::array< uint8_t, 4 > getColorChannel(uint32_t dw_col)
32位颜色获取所有通道,按书写顺序拆分 例如 0x00112233 -> 00, 11, 22, 33
void colorInverse(Color &col)
反色,不包含透明通道
void colorSaturate(Color &col)
限制颜色值 到[0, 1]