|
1 |
| -/* |
2 |
| - * |
3 |
| - * Copyright (c) 2021 Project CHIP Authors |
4 |
| - * All rights reserved. |
5 |
| - * |
6 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
7 |
| - * you may not use this file except in compliance with the License. |
8 |
| - * You may obtain a copy of the License at |
9 |
| - * |
10 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
11 |
| - * |
12 |
| - * Unless required by applicable law or agreed to in writing, software |
13 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
14 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 |
| - * See the License for the specific language governing permissions and |
16 |
| - * limitations under the License. |
17 |
| - */ |
18 |
| - |
19 |
| -#include "ColorFormat.h" |
20 |
| - |
21 |
| -#include <math.h> |
22 |
| - |
23 |
| -// define a clamp macro to substitute the std::clamp macro which is available from C++17 onwards |
24 |
| -#define clamp(a, min, max) ((a) < (min) ? (min) : ((a) > (max) ? (max) : (a))) |
25 |
| - |
26 |
| -RgbColor_t HsvToRgb(HsvColor_t hsv) |
27 |
| -{ |
28 |
| - RgbColor_t rgb; |
29 |
| - |
30 |
| - uint16_t i = hsv.h / 60; |
31 |
| - uint16_t rgb_max = hsv.v; |
32 |
| - uint16_t rgb_min = (uint16_t)(rgb_max * (100 - hsv.s)) / 100; |
33 |
| - uint16_t diff = hsv.h % 60; |
34 |
| - uint16_t rgb_adj = (uint16_t)((rgb_max - rgb_min) * diff) / 60; |
35 |
| - |
36 |
| - switch (i) |
37 |
| - { |
38 |
| - case 0: |
39 |
| - rgb.r = (uint8_t) rgb_max; |
40 |
| - rgb.g = (uint8_t)(rgb_min + rgb_adj); |
41 |
| - rgb.b = (uint8_t) rgb_min; |
42 |
| - break; |
43 |
| - case 1: |
44 |
| - rgb.r = (uint8_t)(rgb_max - rgb_adj); |
45 |
| - rgb.g = (uint8_t) rgb_max; |
46 |
| - rgb.b = (uint8_t) rgb_min; |
47 |
| - break; |
48 |
| - case 2: |
49 |
| - rgb.r = (uint8_t) rgb_min; |
50 |
| - rgb.g = (uint8_t) rgb_max; |
51 |
| - rgb.b = (uint8_t)(rgb_min + rgb_adj); |
52 |
| - break; |
53 |
| - case 3: |
54 |
| - rgb.r = (uint8_t) rgb_min; |
55 |
| - rgb.g = (uint8_t)(rgb_max - rgb_adj); |
56 |
| - rgb.b = (uint8_t) rgb_max; |
57 |
| - break; |
58 |
| - case 4: |
59 |
| - rgb.r = (uint8_t)(rgb_min + rgb_adj); |
60 |
| - rgb.g = (uint8_t) rgb_min; |
61 |
| - rgb.b = (uint8_t) rgb_max; |
62 |
| - break; |
63 |
| - default: |
64 |
| - rgb.r = (uint8_t) rgb_max; |
65 |
| - rgb.g = (uint8_t) rgb_min; |
66 |
| - rgb.b = (uint8_t)(rgb_max - rgb_adj); |
67 |
| - break; |
68 |
| - } |
69 |
| - |
70 |
| - return rgb; |
71 |
| -} |
72 |
| - |
73 |
| -RgbColor_t XYToRgb(uint8_t Level, uint16_t currentX, uint16_t currentY) |
74 |
| -{ |
75 |
| - // convert xyY color space to RGB |
76 |
| - |
77 |
| - // https://www.easyrgb.com/en/math.php |
78 |
| - // https://en.wikipedia.org/wiki/SRGB |
79 |
| - // refer https://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space |
80 |
| - |
81 |
| - // The currentX/currentY attribute contains the current value of the normalized chromaticity value of x/y. |
82 |
| - // The value of x/y shall be related to the currentX/currentY attribute by the relationship |
83 |
| - // x = currentX/65536 |
84 |
| - // y = currentY/65536 |
85 |
| - // z = 1-x-y |
86 |
| - |
87 |
| - RgbColor_t rgb; |
88 |
| - |
89 |
| - float x, y, z; |
90 |
| - float X, Y, Z; |
91 |
| - float r, g, b; |
92 |
| - |
93 |
| - x = ((float) currentX) / 65535.0f; |
94 |
| - y = ((float) currentY) / 65535.0f; |
95 |
| - |
96 |
| - z = 1.0f - x - y; |
97 |
| - |
98 |
| - // Calculate XYZ values |
99 |
| - |
100 |
| - // Y - given brightness in 0 - 1 range |
101 |
| - Y = ((float) Level) / 254.0f; |
102 |
| - X = (Y / y) * x; |
103 |
| - Z = (Y / y) * z; |
104 |
| - |
105 |
| - // X, Y and Z input refer to a D65/2° standard illuminant. |
106 |
| - // sR, sG and sB (standard RGB) output range = 0 ÷ 255 |
107 |
| - // convert XYZ to RGB - CIE XYZ to sRGB |
108 |
| - X = X / 100.0f; |
109 |
| - Y = Y / 100.0f; |
110 |
| - Z = Z / 100.0f; |
111 |
| - |
112 |
| - r = (X * 3.2406f) - (Y * 1.5372f) - (Z * 0.4986f); |
113 |
| - g = -(X * 0.9689f) + (Y * 1.8758f) + (Z * 0.0415f); |
114 |
| - b = (X * 0.0557f) - (Y * 0.2040f) + (Z * 1.0570f); |
115 |
| - |
116 |
| - // apply gamma 2.2 correction |
117 |
| - r = (r <= 0.0031308f ? 12.92f * r : (1.055f) * pow(r, (1.0f / 2.4f)) - 0.055f); |
118 |
| - g = (g <= 0.0031308f ? 12.92f * g : (1.055f) * pow(g, (1.0f / 2.4f)) - 0.055f); |
119 |
| - b = (b <= 0.0031308f ? 12.92f * b : (1.055f) * pow(b, (1.0f / 2.4f)) - 0.055f); |
120 |
| - |
121 |
| - // Round off |
122 |
| - r = clamp(r, 0, 1); |
123 |
| - g = clamp(g, 0, 1); |
124 |
| - b = clamp(b, 0, 1); |
125 |
| - |
126 |
| - // these rgb values are in the range of 0 to 1, convert to limit of HW specific LED |
127 |
| - rgb.r = (uint8_t)(r * 255); |
128 |
| - rgb.g = (uint8_t)(g * 255); |
129 |
| - rgb.b = (uint8_t)(b * 255); |
130 |
| - |
131 |
| - return rgb; |
132 |
| -} |
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2021 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "ColorFormat.h" |
| 20 | + |
| 21 | +#include <math.h> |
| 22 | + |
| 23 | +// define a clamp macro to substitute the std::clamp macro which is available from C++17 onwards |
| 24 | +#define clamp(a, min, max) ((a) < (min) ? (min) : ((a) > (max) ? (max) : (a))) |
| 25 | + |
| 26 | +RgbColor_t HsvToRgb(HsvColor_t hsv) |
| 27 | +{ |
| 28 | + RgbColor_t rgb; |
| 29 | + |
| 30 | + uint16_t i = hsv.h / 60; |
| 31 | + uint16_t rgb_max = hsv.v; |
| 32 | + uint16_t rgb_min = (uint16_t)(rgb_max * (100 - hsv.s)) / 100; |
| 33 | + uint16_t diff = hsv.h % 60; |
| 34 | + uint16_t rgb_adj = (uint16_t)((rgb_max - rgb_min) * diff) / 60; |
| 35 | + |
| 36 | + switch (i) |
| 37 | + { |
| 38 | + case 0: |
| 39 | + rgb.r = (uint8_t) rgb_max; |
| 40 | + rgb.g = (uint8_t)(rgb_min + rgb_adj); |
| 41 | + rgb.b = (uint8_t) rgb_min; |
| 42 | + break; |
| 43 | + case 1: |
| 44 | + rgb.r = (uint8_t)(rgb_max - rgb_adj); |
| 45 | + rgb.g = (uint8_t) rgb_max; |
| 46 | + rgb.b = (uint8_t) rgb_min; |
| 47 | + break; |
| 48 | + case 2: |
| 49 | + rgb.r = (uint8_t) rgb_min; |
| 50 | + rgb.g = (uint8_t) rgb_max; |
| 51 | + rgb.b = (uint8_t)(rgb_min + rgb_adj); |
| 52 | + break; |
| 53 | + case 3: |
| 54 | + rgb.r = (uint8_t) rgb_min; |
| 55 | + rgb.g = (uint8_t)(rgb_max - rgb_adj); |
| 56 | + rgb.b = (uint8_t) rgb_max; |
| 57 | + break; |
| 58 | + case 4: |
| 59 | + rgb.r = (uint8_t)(rgb_min + rgb_adj); |
| 60 | + rgb.g = (uint8_t) rgb_min; |
| 61 | + rgb.b = (uint8_t) rgb_max; |
| 62 | + break; |
| 63 | + default: |
| 64 | + rgb.r = (uint8_t) rgb_max; |
| 65 | + rgb.g = (uint8_t) rgb_min; |
| 66 | + rgb.b = (uint8_t)(rgb_max - rgb_adj); |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + return rgb; |
| 71 | +} |
| 72 | + |
| 73 | +RgbColor_t XYToRgb(uint8_t Level, uint16_t currentX, uint16_t currentY) |
| 74 | +{ |
| 75 | + // convert xyY color space to RGB |
| 76 | + |
| 77 | + // https://www.easyrgb.com/en/math.php |
| 78 | + // https://en.wikipedia.org/wiki/SRGB |
| 79 | + // refer https://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space |
| 80 | + |
| 81 | + // The currentX/currentY attribute contains the current value of the normalized chromaticity value of x/y. |
| 82 | + // The value of x/y shall be related to the currentX/currentY attribute by the relationship |
| 83 | + // x = currentX/65536 |
| 84 | + // y = currentY/65536 |
| 85 | + // z = 1-x-y |
| 86 | + |
| 87 | + RgbColor_t rgb; |
| 88 | + |
| 89 | + float x, y, z; |
| 90 | + float X, Y, Z; |
| 91 | + float r, g, b; |
| 92 | + |
| 93 | + x = ((float) currentX) / 65535.0f; |
| 94 | + y = ((float) currentY) / 65535.0f; |
| 95 | + |
| 96 | + z = 1.0f - x - y; |
| 97 | + |
| 98 | + // Calculate XYZ values |
| 99 | + |
| 100 | + // Y - given brightness in 0 - 1 range |
| 101 | + Y = ((float) Level) / 254.0f; |
| 102 | + X = (Y / y) * x; |
| 103 | + Z = (Y / y) * z; |
| 104 | + |
| 105 | + // X, Y and Z input refer to a D65/2° standard illuminant. |
| 106 | + // sR, sG and sB (standard RGB) output range = 0 ÷ 255 |
| 107 | + // convert XYZ to RGB - CIE XYZ to sRGB |
| 108 | + X = X / 100.0f; |
| 109 | + Y = Y / 100.0f; |
| 110 | + Z = Z / 100.0f; |
| 111 | + |
| 112 | + r = (X * 3.2406f) - (Y * 1.5372f) - (Z * 0.4986f); |
| 113 | + g = -(X * 0.9689f) + (Y * 1.8758f) + (Z * 0.0415f); |
| 114 | + b = (X * 0.0557f) - (Y * 0.2040f) + (Z * 1.0570f); |
| 115 | + |
| 116 | + // apply gamma 2.2 correction |
| 117 | + r = (r <= 0.0031308f ? 12.92f * r : (1.055f) * pow(r, (1.0f / 2.4f)) - 0.055f); |
| 118 | + g = (g <= 0.0031308f ? 12.92f * g : (1.055f) * pow(g, (1.0f / 2.4f)) - 0.055f); |
| 119 | + b = (b <= 0.0031308f ? 12.92f * b : (1.055f) * pow(b, (1.0f / 2.4f)) - 0.055f); |
| 120 | + |
| 121 | + // Round off |
| 122 | + r = clamp(r, 0, 1); |
| 123 | + g = clamp(g, 0, 1); |
| 124 | + b = clamp(b, 0, 1); |
| 125 | + |
| 126 | + // these rgb values are in the range of 0 to 1, convert to limit of HW specific LED |
| 127 | + rgb.r = (uint8_t)(r * 255); |
| 128 | + rgb.g = (uint8_t)(g * 255); |
| 129 | + rgb.b = (uint8_t)(b * 255); |
| 130 | + |
| 131 | + return rgb; |
| 132 | +} |
0 commit comments