-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.c
94 lines (78 loc) · 2.62 KB
/
color.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @file color.c
* @brief color algorithms.
*
* @addtogroup effects
* @{
*/
#include "color.h"
#include <stdlib.h>
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
static uint8_t BlendComponent(uint8_t s, uint8_t d, float factor)
{
int16_t component = (int16_t)s;
component -= (int16_t)d;
component *= factor;
component -= (int16_t)s;
if (component < 0)
{
component *= -1;
}
return component;
}
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Copy one color to another.
*
*/
void ColorCopy(const struct Color* src, struct Color* dst)
{
dst->R = src->R;
dst->G = src->G;
dst->B = src->B;
}
void ColorRandom(struct Color* dst)
{
dst->R = rand() % 256;
dst->G = rand() % 256;
dst->B = rand() % 256;
}
void ColorScale(struct Color* color, float scale)
{
color->R = color->R * scale;
color->G = color->G * scale;
color->B = color->B * scale;
}
void ColorBlend(const struct Color* src, const struct Color* dst, float factor, struct Color* result)
{
result->R = BlendComponent(src->R, dst->R, factor);
result->G = BlendComponent(src->G, dst->G, factor);
result->B = BlendComponent(src->B, dst->B, factor);
}
uint32_t ColorToRGBValue(const struct Color* src)
{
uint32_t value = src->R << 16;
value += src->G << 8;
value += src->B;
return value;
}
void ColorFromRGBValue(uint32_t src, struct Color* dst)
{
dst->R = (src >> 16) & 0x000000ff;
dst->G = (src >> 8) & 0x000000ff;
dst->B = (src >> 0) & 0x000000ff;
}
/** @} */