-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcolor.cpp
61 lines (52 loc) · 1.39 KB
/
color.cpp
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
/*!
@file color.cpp
@author lisper ([email protected])
@license LGPLv3 (see license.txt)
function is hsb to rgb
Copyright (C) 2014 DFRobot
*/
#include "Arduino.h"
#include "color.h"
// hsb to rgb
void HSB2RGB (float *hsb, uint8_t *rgb) {
for(int offset=240,i=0; i<3; i++,offset -= 120) {
float x = fabs (((int)(hsb[0]+offset)) % 360 - 240.0);
if(x<=60)
rgb[i] = 255;
else if(60<x && x<120)
rgb[i] = ((1-(x-60)/60)*255);
else
rgb[i] = 0;
}
for(int i=0; i<3; i++)
rgb[i] += (255-rgb[i]) * (1-hsb[1]);
for(int i=0; i<3; i++)
rgb[i] *= hsb[2];
}
//
uint32_t hsbToColor (float h, float s, float b) {
//return color:0x00rrggbb
//h=>(0~360), s=>(0~1.0) add=>(0~1.0)
float hsb[3] = {
h, s, b};
uint8_t rgb[3];
HSB2RGB (hsb, rgb);
return ((uint32_t)rgb[0] << 16) | ((uint32_t)rgb[1] << 8) | (uint32_t)rgb[2];
}
#ifdef colorCircle
uint32_t hbToColor (uint16_t h, float b) {
//return color:0x00rrggbb, very fast, note: s always is 1.0
//h=>(0~360), b=>(0~1.0)
uint32_t color = color_array[h];
uint8_t r = (uint8_t)color * b;
uint8_t g = (uint8_t)(color>>8) * b;
uint8_t add = (uint8_t)(color>>16) * b;
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | (uint32_t)add;
}
void setColorCircle (uint32_t *array, uint32_t length, float s, float add) {
//array length is 360
for (int i=0; i<length; i++) {
array[i] = get_color_from_hsb (i, s, add);
}
}
#endif