Forum: Mikrocontroller und Digitale Elektronik hsv2rgb 8bit fehlerhaft?


von Simon (Gast)


Lesenswert?

Hallo,

ich habe folgenden Code aus der FastLed-Lib extrahiert. Jedoch habe ich 
das Problem, dass die Berechnungen scheinbar falsch sind und nicht 
funktionieren.

System ist ein Atmega88.
1
#define APPLY_DIMMING(X) (X)
2
#define HSV_SECTION_6 (0x20)
3
#define HSV_SECTION_3 (0x40)
4
void hsv2rgb_raw_C (struct cHSV *hsv, struct cRGB *rgb, int16_t led)
5
{
6
  // Convert hue, saturation and brightness ( HSV/HSB ) to RGB
7
  // "Dimming" is used on saturation and brightness to make
8
  // the output more visually linear.
9
10
  // Apply dimming curves
11
  uint8_t value = APPLY_DIMMING( hsv[led].val);
12
  uint8_t saturation = hsv[led].sat;
13
14
  // The brightness floor is minimum number that all of
15
  // R, G, and B will be set to.
16
  uint8_t invsat = APPLY_DIMMING( 255 - saturation);
17
  uint8_t brightness_floor = (value * invsat) / 256;
18
19
  // The color amplitude is the maximum amount of R, G, and B
20
  // that will be added on top of the brightness_floor to
21
  // create the specific hue desired.
22
  uint8_t color_amplitude = value - brightness_floor;
23
24
  // Figure out which section of the hue wheel we're in,
25
  // and how far offset we are withing that section
26
  uint8_t section = hsv[led].hue / HSV_SECTION_3; // 0..2
27
  uint8_t offset = hsv[led].hue % HSV_SECTION_3;  // 0..63
28
29
  uint8_t rampup = offset; // 0..63
30
  uint8_t rampdown = (HSV_SECTION_3 - 1) - offset; // 63..0
31
32
  // We now scale rampup and rampdown to a 0-255 range -- at least
33
  // in theory, but here's where architecture-specific decsions
34
  // come in to play:
35
  // To scale them up to 0-255, we'd want to multiply by 4.
36
  // But in the very next step, we multiply the ramps by other
37
  // values and then divide the resulting product by 256.
38
  // So which is faster?
39
  //   ((ramp * 4) * othervalue) / 256
40
  // or
41
  //   ((ramp    ) * othervalue) /  64
42
  // It depends on your processor architecture.
43
  // On 8-bit AVR, the "/ 256" is just a one-cycle register move,
44
  // but the "/ 64" might be a multicycle shift process. So on AVR
45
  // it's faster do multiply the ramp values by four, and then
46
  // divide by 256.
47
  // On ARM, the "/ 256" and "/ 64" are one cycle each, so it's
48
  // faster to NOT multiply the ramp values by four, and just to
49
  // divide the resulting product by 64 (instead of 256).
50
  // Moral of the story: trust your profiler, not your insticts.
51
52
  // Since there's an AVR assembly version elsewhere, we'll
53
  // assume what we're on an architecture where any number of
54
  // bit shifts has roughly the same cost, and we'll remove the
55
  // redundant math at the source level:
56
57
  //  // scale up to 255 range
58
  //  //rampup *= 4; // 0..252
59
  //  //rampdown *= 4; // 0..252
60
61
  // compute color-amplitude-scaled-down versions of rampup and rampdown
62
  uint8_t rampup_amp_adj   = (rampup   * color_amplitude) / (256 / 4);
63
  uint8_t rampdown_amp_adj = (rampdown * color_amplitude) / (256 / 4);
64
65
  // add brightness_floor offset to everything
66
  uint8_t rampup_adj_with_floor   = rampup_amp_adj   + brightness_floor;
67
  uint8_t rampdown_adj_with_floor = rampdown_amp_adj + brightness_floor;
68
69
70
  if( section ) {
71
    if( section == 1) {
72
      // section 1: 0x40..0x7F
73
      rgb[led].r = brightness_floor;
74
      rgb[led].g = rampdown_adj_with_floor;
75
      rgb[led].b = rampup_adj_with_floor;
76
      } else {
77
      // section 2; 0x80..0xBF
78
      rgb[led].r = rampup_adj_with_floor;
79
      rgb[led].g = brightness_floor;
80
      rgb[led].b = rampdown_adj_with_floor;
81
    }
82
    } else {
83
    // section 0: 0x00..0x3F
84
    rgb[led].r = rampdown_adj_with_floor;
85
    rgb[led].g = rampup_adj_with_floor;
86
    rgb[led].b = brightness_floor;
87
  }
88
}

Diese Umrechnung kenne ich auch, möchte aber floats vermeiden. 
http://www.ulrichradig.de/home/index.php/projekte/hsv-to-rgb-led

von Simon (Gast)


Lesenswert?

So wie ich das sehe, geht der HSV Bereich nur bis 191.

von Wolfgang (Gast)


Lesenswert?

Simon schrieb:
> Jedoch habe ich das Problem, dass die Berechnungen scheinbar falsch sind
> und nicht funktionieren.

Was heißt "scheinbar falsch". Entweder sind sie falsch oder nicht.

 - wie sieht dein Input aus?
 - wie sieht dein Output aus?
 - was kommt raus, wenn man von Hand nachrechnet?

von Stefan S. (sschultewolter)


Lesenswert?

Hallo,

hab das von dir oben geteilte Programm einmal auf dem PC getestet. Werte 
sollten eigentlich korrekt sein, also von 0-255.



1
#include "stdafx.h"
2
3
struct cRGB { uint8_t r; uint8_t g; uint8_t b; };
4
struct cHSV { uint8_t h; uint8_t s; uint8_t v; };
5
6
const uint8_t NUM_LEDS = 6;
7
struct cRGB rgb[NUM_LEDS];
8
struct cHSV hsv[NUM_LEDS];
9
10
#define APPLY_DIMMING(X) (X)
11
#define HSV_SECTION_6 (0x55)
12
#define HSV_SECTION_3 (0x40)
13
void hsv2rgb_raw_C(struct cRGB *rgb, struct cHSV *hsv, int16_t led)
14
{
15
  // Convert hue, saturation and brightness ( HSV/HSB ) to RGB
16
  // "Dimming" is used on saturation and brightness to make
17
  // the output more visually linear.
18
19
  // Apply dimming curves
20
  uint8_t value = hsv[led].v;
21
  uint8_t saturation = hsv[led].s;
22
23
  // The brightness floor is minimum number that all of
24
  // R, G, and B will be set to.
25
  uint8_t invsat = APPLY_DIMMING(255 - saturation);
26
  uint8_t brightness_floor = (value * invsat) / 256;
27
28
  // The color amplitude is the maximum amount of R, G, and B
29
  // that will be added on top of the brightness_floor to
30
  // create the specific hue desired.
31
  uint8_t color_amplitude = value - brightness_floor;
32
33
  // Figure out which section of the hue wheel we're in,
34
  // and how far offset we are withing that section
35
  uint8_t section = hsv[led].h / HSV_SECTION_3; // 0..2
36
  uint8_t offset = hsv[led].h % HSV_SECTION_3;  // 0..63
37
38
  uint8_t rampup = offset; // 0..63
39
  uint8_t rampdown = (HSV_SECTION_3 - 1) - offset; // 63..0
40
41
                           // We now scale rampup and rampdown to a 0-255 range -- at least
42
                           // in theory, but here's where architecture-specific decsions
43
                           // come in to play:
44
                           // To scale them up to 0-255, we'd want to multiply by 4.
45
                           // But in the very next step, we multiply the ramps by other
46
                           // values and then divide the resulting product by 256.
47
                           // So which is faster?
48
                           //   ((ramp * 4) * othervalue) / 256
49
                           // or
50
                           //   ((ramp    ) * othervalue) /  64
51
                           // It depends on your processor architecture.
52
                           // On 8-bit AVR, the "/ 256" is just a one-cycle register move,
53
                           // but the "/ 64" might be a multicycle shift process. So on AVR
54
                           // it's faster do multiply the ramp values by four, and then
55
                           // divide by 256.
56
                           // On ARM, the "/ 256" and "/ 64" are one cycle each, so it's
57
                           // faster to NOT multiply the ramp values by four, and just to
58
                           // divide the resulting product by 64 (instead of 256).
59
                           // Moral of the story: trust your profiler, not your insticts.
60
61
                           // Since there's an AVR assembly version elsewhere, we'll
62
                           // assume what we're on an architecture where any number of
63
                           // bit shifts has roughly the same cost, and we'll remove the
64
                           // redundant math at the source level:
65
66
                           //  // scale up to 255 range
67
                           //  //rampup *= 4; // 0..252
68
                           //  //rampdown *= 4; // 0..252
69
70
                           // compute color-amplitude-scaled-down versions of rampup and rampdown
71
  uint8_t rampup_amp_adj = (rampup   * color_amplitude) / (256 / 4);
72
  uint8_t rampdown_amp_adj = (rampdown * color_amplitude) / (256 / 4);
73
74
  // add brightness_floor offset to everything
75
  uint8_t rampup_adj_with_floor = rampup_amp_adj + brightness_floor;
76
  uint8_t rampdown_adj_with_floor = rampdown_amp_adj + brightness_floor;
77
78
79
  if (section) {
80
    if (section == 1) {
81
      // section 1: 0x40..0x7F
82
      rgb[led].r = brightness_floor;
83
      rgb[led].g = rampdown_adj_with_floor;
84
      rgb[led].b = rampup_adj_with_floor;
85
    }
86
    else {
87
      // section 2; 0x80..0xBF
88
      rgb[led].r = rampup_adj_with_floor;
89
      rgb[led].g = brightness_floor;
90
      rgb[led].b = rampdown_adj_with_floor;
91
    }
92
  }
93
  else {
94
    // section 0: 0x00..0x3F
95
    rgb[led].r = rampdown_adj_with_floor;
96
    rgb[led].g = rampup_adj_with_floor;
97
    rgb[led].b = brightness_floor;
98
  }
99
}
100
101
int main()
102
{
103
  const uint8_t color[6] = { 0, 42, 85, 127, 170, 212 };
104
105
  printf("-- hue 0-255, sat 255, val 255\n");
106
  for (uint8_t i = 0; i < NUM_LEDS; i++)
107
  {
108
    hsv[i].h = color[i];
109
    hsv[i].s = 255;
110
    hsv[i].v = 255;
111
112
    hsv2rgb_raw_C(rgb, hsv, i);
113
    printf("hue %3d:\t%3d\t%3d\t%3d\n", hsv[i].h,  rgb[i].r, rgb[i].g, rgb[i].b);
114
  }
115
116
  printf("\n");
117
  printf("-- hue 0-255, sat 0, val 255\n");
118
  for (uint8_t i = 0; i < NUM_LEDS; i++)
119
  {
120
    hsv[i].h = color[i];
121
    hsv[i].s = 0;
122
    hsv[i].v = 255;
123
124
    hsv2rgb_raw_C(rgb, hsv, i);
125
    printf("hue %3d:\t%3d\t%3d\t%3d\n", hsv[i].h, rgb[i].r, rgb[i].g, rgb[i].b);
126
  }
127
128
  while (1);
129
  return 0;
130
}
1
-- hue 0-255, sat 255, val 255
2
hue   0:        251       0       0
3
hue  42:         83     167       0
4
hue  85:          0     167      83
5
hue 127:          0       0     251
6
hue 170:        167       0      83
7
hue 212:         79       0     171
8
9
-- hue 0-255, sat 0, val 255
10
hue   0:        254     254     254
11
hue  42:        254     254     254
12
hue  85:        254     254     254
13
hue 127:        254     254     254
14
hue 170:        254     254     254
15
hue 212:        254     254     254

von Stefan S. (sschultewolter)


Lesenswert?

Ich sehe gerade, das die Berechnungen so nicht funktionieren. Das ergibt 
keine ordentliche RGB Kurve. Desweiteren kommt es bei einigen Werten zu 
einem Überlauf. Habe gerade keine Zeit, dass ganze als Diagramm zu 
posten, aber es sollte ersichtlich sein, dass die Berechnung so nicht 
funktioniert. Ggf. hat ein anderer einen Tipp, wieso die Berechnung 
nicht Funktioniert.
1
Alle Berechnungen mit inthue:   0        334      0       0
2
hue:   1         330      3       0
3
hue:   2         326      7       0
4
hue:   3         322     11       0
5
hue:   4         318     15       0
6
hue:   5         314     19       0
7
hue:   6         310     23       0
8
hue:   7         306     27       0
9
hue:   8         302     31       0
10
hue:   9         298     35       0
11
hue:  10         294     39       0
12
hue:  11         290     43       0
13
hue:  12         286     47       0
14
hue:  13         282     51       0
15
hue:  14         278     55       0
16
hue:  15         274     59       0
17
hue:  16         270     63       0
18
hue:  17         266     67       0
19
hue:  18         262     71       0
20
hue:  19         258     75       0
21
hue:  20         255     79       0
22
hue:  21         251     83       0
23
hue:  22         247     87       0
24
hue:  23         243     91       0
25
hue:  24         239     95       0
26
hue:  25         235     99       0
27
hue:  26         231     103      0
28
hue:  27         227     107      0
29
hue:  28         223     111      0
30
hue:  29         219     115      0
31
hue:  30         215     119      0
32
hue:  31         211     123      0
33
hue:  32         207     127      0
34
hue:  33         203     131      0
35
hue:  34         199     135      0
36
hue:  35         195     139      0
37
hue:  36         191     143      0
38
hue:  37         187     147      0
39
hue:  38         183     151      0
40
hue:  39         179     155      0
41
hue:  40         175     159      0
42
hue:  41         171     163      0
43
hue:  42         167     167      0
44
hue:  43         163     171      0
45
hue:  44         159     175      0
46
hue:  45         155     179      0
47
hue:  46         151     183      0
48
hue:  47         147     187      0
49
hue:  48         143     191      0
50
hue:  49         139     195      0
51
hue:  50         135     199      0
52
hue:  51         131     203      0
53
hue:  52         127     207      0
54
hue:  53         123     211      0
55
hue:  54         119     215      0
56
hue:  55         115     219      0
57
hue:  56         111     223      0
58
hue:  57         107     227      0
59
hue:  58         103     231      0
60
hue:  59         99      235      0
61
hue:  60         95      239      0
62
hue:  61         91      243      0
63
hue:  62         87      247      0
64
hue:  63         83      251      0
65
hue:  64         79      255      0
66
hue:  65         75      258      0
67
hue:  66         71      262      0
68
hue:  67         67      266      0
69
hue:  68         63      270      0
70
hue:  69         59      274      0
71
hue:  70         55      278      0
72
hue:  71         51      282      0
73
hue:  72         47      286      0
74
hue:  73         43      290      0
75
hue:  74         39      294      0
76
hue:  75         35      298      0
77
hue:  76         31      302      0
78
hue:  77         27      306      0
79
hue:  78         23      310      0
80
hue:  79         19      314      0
81
hue:  80         15      318      0
82
hue:  81         11      322      0
83
hue:  82          7      326      0
84
hue:  83          3      330      0
85
hue:  84          0      334      0
86
hue:  85          0      334      0
87
hue:  86          0      330      3
88
hue:  87          0      326      7
89
hue:  88          0      322     11
90
hue:  89          0      318     15
91
hue:  90          0      314     19
92
hue:  91          0      310     23
93
hue:  92          0      306     27
94
hue:  93          0      302     31
95
hue:  94          0      298     35
96
hue:  95          0      294     39
97
hue:  96          0      290     43
98
hue:  97          0      286     47
99
hue:  98          0      282     51
100
hue:  99          0      278     55
101
hue:  100         0      274     59
102
hue:  101         0      270     63
103
hue:  102         0      266     67
104
hue:  103         0      262     71
105
hue:  104         0      258     75
106
hue:  105         0      255     79
107
hue:  106         0      251     83
108
hue:  107         0      247     87
109
hue:  108         0      243     91
110
hue:  109         0      239     95
111
hue:  110         0      235     99
112
hue:  111         0      231     103
113
hue:  112         0      227     107
114
hue:  113         0      223     111
115
hue:  114         0      219     115
116
hue:  115         0      215     119
117
hue:  116         0      211     123
118
hue:  117         0      207     127
119
hue:  118         0      203     131
120
hue:  119         0      199     135
121
hue:  120         0      195     139
122
hue:  121         0      191     143
123
hue:  122         0      187     147
124
hue:  123         0      183     151
125
hue:  124         0      179     155
126
hue:  125         0      175     159
127
hue:  126         0      171     163
128
hue:  127         0      167     167
129
hue:  128         0      163     171
130
hue:  129         0      159     175
131
hue:  130         0      155     179
132
hue:  131         0      151     183
133
hue:  132         0      147     187
134
hue:  133         0      143     191
135
hue:  134         0      139     195
136
hue:  135         0      135     199
137
hue:  136         0      131     203
138
hue:  137         0      127     207
139
hue:  138         0      123     211
140
hue:  139         0      119     215
141
hue:  140         0      115     219
142
hue:  141         0      111     223
143
hue:  142         0      107     227
144
hue:  143         0      103     231
145
hue:  144         0      99      235
146
hue:  145         0      95      239
147
hue:  146         0      91      243
148
hue:  147         0      87      247
149
hue:  148         0      83      251
150
hue:  149         0      79      255
151
hue:  150         0      75      258
152
hue:  151         0      71      262
153
hue:  152         0      67      266
154
hue:  153         0      63      270
155
hue:  154         0      59      274
156
hue:  155         0      55      278
157
hue:  156         0      51      282
158
hue:  157         0      47      286
159
hue:  158         0      43      290
160
hue:  159         0      39      294
161
hue:  160         0      35      298
162
hue:  161         0      31      302
163
hue:  162         0      27      306
164
hue:  163         0      23      310
165
hue:  164         0      19      314
166
hue:  165         0      15      318
167
hue:  166         0      11      322
168
hue:  167         0       7      326
169
hue:  168         0       3      330
170
hue:  169         0       0      334
171
hue:  170         0       0      334
172
hue:  171         3       0      330
173
hue:  172         7       0      326
174
hue:  173        11       0      322
175
hue:  174        15       0      318
176
hue:  175        19       0      314
177
hue:  176        23       0      310
178
hue:  177        27       0      306
179
hue:  178        31       0      302
180
hue:  179        35       0      298
181
hue:  180        39       0      294
182
hue:  181        43       0      290
183
hue:  182        47       0      286
184
hue:  183        51       0      282
185
hue:  184        55       0      278
186
hue:  185        59       0      274
187
hue:  186        63       0      270
188
hue:  187        67       0      266
189
hue:  188        71       0      262
190
hue:  189        75       0      258
191
hue:  190        79       0      255
192
hue:  191        83       0      251
193
hue:  192        87       0      247
194
hue:  193        91       0      243
195
hue:  194        95       0      239
196
hue:  195        99       0      235
197
hue:  196        103      0      231
198
hue:  197        107      0      227
199
hue:  198        111      0      223
200
hue:  199        115      0      219
201
hue:  200        119      0      215
202
hue:  201        123      0      211
203
hue:  202        127      0      207
204
hue:  203        131      0      203
205
hue:  204        135      0      199
206
hue:  205        139      0      195
207
hue:  206        143      0      191
208
hue:  207        147      0      187
209
hue:  208        151      0      183
210
hue:  209        155      0      179
211
hue:  210        159      0      175
212
hue:  211        163      0      171
213
hue:  212        167      0      167
214
hue:  213        171      0      163
215
hue:  214        175      0      159
216
hue:  215        179      0      155
217
hue:  216        183      0      151
218
hue:  217        187      0      147
219
hue:  218        191      0      143
220
hue:  219        195      0      139
221
hue:  220        199      0      135
222
hue:  221        203      0      131
223
hue:  222        207      0      127
224
hue:  223        211      0      123
225
hue:  224        215      0      119
226
hue:  225        219      0      115
227
hue:  226        223      0      111
228
hue:  227        227      0      107
229
hue:  228        231      0      103
230
hue:  229        235      0      99
231
hue:  230        239      0      95
232
hue:  231        243      0      91
233
hue:  232        247      0      87
234
hue:  233        251      0      83
235
hue:  234        255      0      79
236
hue:  235        258      0      75
237
hue:  236        262      0      71
238
hue:  237        266      0      67
239
hue:  238        270      0      63
240
hue:  239        274      0      59
241
hue:  240        278      0      55
242
hue:  241        282      0      51
243
hue:  242        286      0      47
244
hue:  243        290      0      43
245
hue:  244        294      0      39
246
hue:  245        298      0      35
247
hue:  246        302      0      31
248
hue:  247        306      0      27
249
hue:  248        310      0      23
250
hue:  249        314      0      19
251
hue:  250        318      0      15
252
hue:  251        322      0      11
253
hue:  252        326      0       7
254
hue:  253        330      0       3
255
hue:  254        334      0       0
256
hue:  255        334      0       0

1
Alle Berechnungen mit int und MOD256
2
hue:   0         78       0       0
3
hue:   1         74       3       0
4
hue:   2         70       7       0
5
hue:   3         66      11       0
6
hue:   4         62      15       0
7
hue:   5         58      19       0
8
hue:   6         54      23       0
9
hue:   7         50      27       0
10
hue:   8         46      31       0
11
hue:   9         42      35       0
12
hue:  10         38      39       0
13
hue:  11         34      43       0
14
hue:  12         30      47       0
15
hue:  13         26      51       0
16
hue:  14         22      55       0
17
hue:  15         18      59       0
18
hue:  16         14      63       0
19
hue:  17         10      67       0
20
hue:  18          6      71       0
21
hue:  19          2      75       0
22
hue:  20         255     79       0
23
hue:  21         251     83       0
24
hue:  22         247     87       0
25
hue:  23         243     91       0
26
hue:  24         239     95       0
27
hue:  25         235     99       0
28
hue:  26         231     103      0
29
hue:  27         227     107      0
30
hue:  28         223     111      0
31
hue:  29         219     115      0
32
hue:  30         215     119      0
33
hue:  31         211     123      0
34
hue:  32         207     127      0
35
hue:  33         203     131      0
36
hue:  34         199     135      0
37
hue:  35         195     139      0
38
hue:  36         191     143      0
39
hue:  37         187     147      0
40
hue:  38         183     151      0
41
hue:  39         179     155      0
42
hue:  40         175     159      0
43
hue:  41         171     163      0
44
hue:  42         167     167      0
45
hue:  43         163     171      0
46
hue:  44         159     175      0
47
hue:  45         155     179      0
48
hue:  46         151     183      0
49
hue:  47         147     187      0
50
hue:  48         143     191      0
51
hue:  49         139     195      0
52
hue:  50         135     199      0
53
hue:  51         131     203      0
54
hue:  52         127     207      0
55
hue:  53         123     211      0
56
hue:  54         119     215      0
57
hue:  55         115     219      0
58
hue:  56         111     223      0
59
hue:  57         107     227      0
60
hue:  58         103     231      0
61
hue:  59         99      235      0
62
hue:  60         95      239      0
63
hue:  61         91      243      0
64
hue:  62         87      247      0
65
hue:  63         83      251      0
66
hue:  64         79      255      0
67
hue:  65         75       2       0
68
hue:  66         71       6       0
69
hue:  67         67      10       0
70
hue:  68         63      14       0
71
hue:  69         59      18       0
72
hue:  70         55      22       0
73
hue:  71         51      26       0
74
hue:  72         47      30       0
75
hue:  73         43      34       0
76
hue:  74         39      38       0
77
hue:  75         35      42       0
78
hue:  76         31      46       0
79
hue:  77         27      50       0
80
hue:  78         23      54       0
81
hue:  79         19      58       0
82
hue:  80         15      62       0
83
hue:  81         11      66       0
84
hue:  82          7      70       0
85
hue:  83          3      74       0
86
hue:  84          0      78       0
87
hue:  85          0      78       0
88
hue:  86          0      74       3
89
hue:  87          0      70       7
90
hue:  88          0      66      11
91
hue:  89          0      62      15
92
hue:  90          0      58      19
93
hue:  91          0      54      23
94
hue:  92          0      50      27
95
hue:  93          0      46      31
96
hue:  94          0      42      35
97
hue:  95          0      38      39
98
hue:  96          0      34      43
99
hue:  97          0      30      47
100
hue:  98          0      26      51
101
hue:  99          0      22      55
102
hue:  100         0      18      59
103
hue:  101         0      14      63
104
hue:  102         0      10      67
105
hue:  103         0       6      71
106
hue:  104         0       2      75
107
hue:  105         0      255     79
108
hue:  106         0      251     83
109
hue:  107         0      247     87
110
hue:  108         0      243     91
111
hue:  109         0      239     95
112
hue:  110         0      235     99
113
hue:  111         0      231     103
114
hue:  112         0      227     107
115
hue:  113         0      223     111
116
hue:  114         0      219     115
117
hue:  115         0      215     119
118
hue:  116         0      211     123
119
hue:  117         0      207     127
120
hue:  118         0      203     131
121
hue:  119         0      199     135
122
hue:  120         0      195     139
123
hue:  121         0      191     143
124
hue:  122         0      187     147
125
hue:  123         0      183     151
126
hue:  124         0      179     155
127
hue:  125         0      175     159
128
hue:  126         0      171     163
129
hue:  127         0      167     167
130
hue:  128         0      163     171
131
hue:  129         0      159     175
132
hue:  130         0      155     179
133
hue:  131         0      151     183
134
hue:  132         0      147     187
135
hue:  133         0      143     191
136
hue:  134         0      139     195
137
hue:  135         0      135     199
138
hue:  136         0      131     203
139
hue:  137         0      127     207
140
hue:  138         0      123     211
141
hue:  139         0      119     215
142
hue:  140         0      115     219
143
hue:  141         0      111     223
144
hue:  142         0      107     227
145
hue:  143         0      103     231
146
hue:  144         0      99      235
147
hue:  145         0      95      239
148
hue:  146         0      91      243
149
hue:  147         0      87      247
150
hue:  148         0      83      251
151
hue:  149         0      79      255
152
hue:  150         0      75       2
153
hue:  151         0      71       6
154
hue:  152         0      67      10
155
hue:  153         0      63      14
156
hue:  154         0      59      18
157
hue:  155         0      55      22
158
hue:  156         0      51      26
159
hue:  157         0      47      30
160
hue:  158         0      43      34
161
hue:  159         0      39      38
162
hue:  160         0      35      42
163
hue:  161         0      31      46
164
hue:  162         0      27      50
165
hue:  163         0      23      54
166
hue:  164         0      19      58
167
hue:  165         0      15      62
168
hue:  166         0      11      66
169
hue:  167         0       7      70
170
hue:  168         0       3      74
171
hue:  169         0       0      78
172
hue:  170         0       0      78
173
hue:  171         3       0      74
174
hue:  172         7       0      70
175
hue:  173        11       0      66
176
hue:  174        15       0      62
177
hue:  175        19       0      58
178
hue:  176        23       0      54
179
hue:  177        27       0      50
180
hue:  178        31       0      46
181
hue:  179        35       0      42
182
hue:  180        39       0      38
183
hue:  181        43       0      34
184
hue:  182        47       0      30
185
hue:  183        51       0      26
186
hue:  184        55       0      22
187
hue:  185        59       0      18
188
hue:  186        63       0      14
189
hue:  187        67       0      10
190
hue:  188        71       0       6
191
hue:  189        75       0       2
192
hue:  190        79       0      255
193
hue:  191        83       0      251
194
hue:  192        87       0      247
195
hue:  193        91       0      243
196
hue:  194        95       0      239
197
hue:  195        99       0      235
198
hue:  196        103      0      231
199
hue:  197        107      0      227
200
hue:  198        111      0      223
201
hue:  199        115      0      219
202
hue:  200        119      0      215
203
hue:  201        123      0      211
204
hue:  202        127      0      207
205
hue:  203        131      0      203
206
hue:  204        135      0      199
207
hue:  205        139      0      195
208
hue:  206        143      0      191
209
hue:  207        147      0      187
210
hue:  208        151      0      183
211
hue:  209        155      0      179
212
hue:  210        159      0      175
213
hue:  211        163      0      171
214
hue:  212        167      0      167
215
hue:  213        171      0      163
216
hue:  214        175      0      159
217
hue:  215        179      0      155
218
hue:  216        183      0      151
219
hue:  217        187      0      147
220
hue:  218        191      0      143
221
hue:  219        195      0      139
222
hue:  220        199      0      135
223
hue:  221        203      0      131
224
hue:  222        207      0      127
225
hue:  223        211      0      123
226
hue:  224        215      0      119
227
hue:  225        219      0      115
228
hue:  226        223      0      111
229
hue:  227        227      0      107
230
hue:  228        231      0      103
231
hue:  229        235      0      99
232
hue:  230        239      0      95
233
hue:  231        243      0      91
234
hue:  232        247      0      87
235
hue:  233        251      0      83
236
hue:  234        255      0      79
237
hue:  235         2       0      75
238
hue:  236         6       0      71
239
hue:  237        10       0      67
240
hue:  238        14       0      63
241
hue:  239        18       0      59
242
hue:  240        22       0      55
243
hue:  241        26       0      51
244
hue:  242        30       0      47
245
hue:  243        34       0      43
246
hue:  244        38       0      39
247
hue:  245        42       0      35
248
hue:  246        46       0      31
249
hue:  247        50       0      27
250
hue:  248        54       0      23
251
hue:  249        58       0      19
252
hue:  250        62       0      15
253
hue:  251        66       0      11
254
hue:  252        70       0       7
255
hue:  253        74       0       3
256
hue:  254        78       0       0
257
hue:  255        78       0       0

1
Alle Berechnungen mit uint8_t
2
hue:   0         78       0       0
3
hue:   1         74       3       0
4
hue:   2         70       7       0
5
hue:   3         66      11       0
6
hue:   4         62      15       0
7
hue:   5         58      19       0
8
hue:   6         54      23       0
9
hue:   7         50      27       0
10
hue:   8         46      31       0
11
hue:   9         42      35       0
12
hue:  10         38      39       0
13
hue:  11         34      43       0
14
hue:  12         30      47       0
15
hue:  13         26      51       0
16
hue:  14         22      55       0
17
hue:  15         18      59       0
18
hue:  16         14      63       0
19
hue:  17         10      67       0
20
hue:  18          6      71       0
21
hue:  19          2      75       0
22
hue:  20         255     79       0
23
hue:  21         251     83       0
24
hue:  22         247     87       0
25
hue:  23         243     91       0
26
hue:  24         239     95       0
27
hue:  25         235     99       0
28
hue:  26         231     103      0
29
hue:  27         227     107      0
30
hue:  28         223     111      0
31
hue:  29         219     115      0
32
hue:  30         215     119      0
33
hue:  31         211     123      0
34
hue:  32         207     127      0
35
hue:  33         203     131      0
36
hue:  34         199     135      0
37
hue:  35         195     139      0
38
hue:  36         191     143      0
39
hue:  37         187     147      0
40
hue:  38         183     151      0
41
hue:  39         179     155      0
42
hue:  40         175     159      0
43
hue:  41         171     163      0
44
hue:  42         167     167      0
45
hue:  43         163     171      0
46
hue:  44         159     175      0
47
hue:  45         155     179      0
48
hue:  46         151     183      0
49
hue:  47         147     187      0
50
hue:  48         143     191      0
51
hue:  49         139     195      0
52
hue:  50         135     199      0
53
hue:  51         131     203      0
54
hue:  52         127     207      0
55
hue:  53         123     211      0
56
hue:  54         119     215      0
57
hue:  55         115     219      0
58
hue:  56         111     223      0
59
hue:  57         107     227      0
60
hue:  58         103     231      0
61
hue:  59         99      235      0
62
hue:  60         95      239      0
63
hue:  61         91      243      0
64
hue:  62         87      247      0
65
hue:  63         83      251      0
66
hue:  64         79      255      0
67
hue:  65         75       2       0
68
hue:  66         71       6       0
69
hue:  67         67      10       0
70
hue:  68         63      14       0
71
hue:  69         59      18       0
72
hue:  70         55      22       0
73
hue:  71         51      26       0
74
hue:  72         47      30       0
75
hue:  73         43      34       0
76
hue:  74         39      38       0
77
hue:  75         35      42       0
78
hue:  76         31      46       0
79
hue:  77         27      50       0
80
hue:  78         23      54       0
81
hue:  79         19      58       0
82
hue:  80         15      62       0
83
hue:  81         11      66       0
84
hue:  82          7      70       0
85
hue:  83          3      74       0
86
hue:  84          0      78       0
87
hue:  85          0      78       0
88
hue:  86          0      74       3
89
hue:  87          0      70       7
90
hue:  88          0      66      11
91
hue:  89          0      62      15
92
hue:  90          0      58      19
93
hue:  91          0      54      23
94
hue:  92          0      50      27
95
hue:  93          0      46      31
96
hue:  94          0      42      35
97
hue:  95          0      38      39
98
hue:  96          0      34      43
99
hue:  97          0      30      47
100
hue:  98          0      26      51
101
hue:  99          0      22      55
102
hue:  100         0      18      59
103
hue:  101         0      14      63
104
hue:  102         0      10      67
105
hue:  103         0       6      71
106
hue:  104         0       2      75
107
hue:  105         0      255     79
108
hue:  106         0      251     83
109
hue:  107         0      247     87
110
hue:  108         0      243     91
111
hue:  109         0      239     95
112
hue:  110         0      235     99
113
hue:  111         0      231     103
114
hue:  112         0      227     107
115
hue:  113         0      223     111
116
hue:  114         0      219     115
117
hue:  115         0      215     119
118
hue:  116         0      211     123
119
hue:  117         0      207     127
120
hue:  118         0      203     131
121
hue:  119         0      199     135
122
hue:  120         0      195     139
123
hue:  121         0      191     143
124
hue:  122         0      187     147
125
hue:  123         0      183     151
126
hue:  124         0      179     155
127
hue:  125         0      175     159
128
hue:  126         0      171     163
129
hue:  127         0      167     167
130
hue:  128         0      163     171
131
hue:  129         0      159     175
132
hue:  130         0      155     179
133
hue:  131         0      151     183
134
hue:  132         0      147     187
135
hue:  133         0      143     191
136
hue:  134         0      139     195
137
hue:  135         0      135     199
138
hue:  136         0      131     203
139
hue:  137         0      127     207
140
hue:  138         0      123     211
141
hue:  139         0      119     215
142
hue:  140         0      115     219
143
hue:  141         0      111     223
144
hue:  142         0      107     227
145
hue:  143         0      103     231
146
hue:  144         0      99      235
147
hue:  145         0      95      239
148
hue:  146         0      91      243
149
hue:  147         0      87      247
150
hue:  148         0      83      251
151
hue:  149         0      79      255
152
hue:  150         0      75       2
153
hue:  151         0      71       6
154
hue:  152         0      67      10
155
hue:  153         0      63      14
156
hue:  154         0      59      18
157
hue:  155         0      55      22
158
hue:  156         0      51      26
159
hue:  157         0      47      30
160
hue:  158         0      43      34
161
hue:  159         0      39      38
162
hue:  160         0      35      42
163
hue:  161         0      31      46
164
hue:  162         0      27      50
165
hue:  163         0      23      54
166
hue:  164         0      19      58
167
hue:  165         0      15      62
168
hue:  166         0      11      66
169
hue:  167         0       7      70
170
hue:  168         0       3      74
171
hue:  169         0       0      78
172
hue:  170         0       0      78
173
hue:  171         3       0      74
174
hue:  172         7       0      70
175
hue:  173        11       0      66
176
hue:  174        15       0      62
177
hue:  175        19       0      58
178
hue:  176        23       0      54
179
hue:  177        27       0      50
180
hue:  178        31       0      46
181
hue:  179        35       0      42
182
hue:  180        39       0      38
183
hue:  181        43       0      34
184
hue:  182        47       0      30
185
hue:  183        51       0      26
186
hue:  184        55       0      22
187
hue:  185        59       0      18
188
hue:  186        63       0      14
189
hue:  187        67       0      10
190
hue:  188        71       0       6
191
hue:  189        75       0       2
192
hue:  190        79       0      255
193
hue:  191        83       0      251
194
hue:  192        87       0      247
195
hue:  193        91       0      243
196
hue:  194        95       0      239
197
hue:  195        99       0      235
198
hue:  196        103      0      231
199
hue:  197        107      0      227
200
hue:  198        111      0      223
201
hue:  199        115      0      219
202
hue:  200        119      0      215
203
hue:  201        123      0      211
204
hue:  202        127      0      207
205
hue:  203        131      0      203
206
hue:  204        135      0      199
207
hue:  205        139      0      195
208
hue:  206        143      0      191
209
hue:  207        147      0      187
210
hue:  208        151      0      183
211
hue:  209        155      0      179
212
hue:  210        159      0      175
213
hue:  211        163      0      171
214
hue:  212        167      0      167
215
hue:  213        171      0      163
216
hue:  214        175      0      159
217
hue:  215        179      0      155
218
hue:  216        183      0      151
219
hue:  217        187      0      147
220
hue:  218        191      0      143
221
hue:  219        195      0      139
222
hue:  220        199      0      135
223
hue:  221        203      0      131
224
hue:  222        207      0      127
225
hue:  223        211      0      123
226
hue:  224        215      0      119
227
hue:  225        219      0      115
228
hue:  226        223      0      111
229
hue:  227        227      0      107
230
hue:  228        231      0      103
231
hue:  229        235      0      99
232
hue:  230        239      0      95
233
hue:  231        243      0      91
234
hue:  232        247      0      87
235
hue:  233        251      0      83
236
hue:  234        255      0      79
237
hue:  235         2       0      75
238
hue:  236         6       0      71
239
hue:  237        10       0      67
240
hue:  238        14       0      63
241
hue:  239        18       0      59
242
hue:  240        22       0      55
243
hue:  241        26       0      51
244
hue:  242        30       0      47
245
hue:  243        34       0      43
246
hue:  244        38       0      39
247
hue:  245        42       0      35
248
hue:  246        46       0      31
249
hue:  247        50       0      27
250
hue:  248        54       0      23
251
hue:  249        58       0      19
252
hue:  250        62       0      15
253
hue:  251        66       0      11
254
hue:  252        70       0       7
255
hue:  253        74       0       3
256
hue:  254        78       0       0
257
hue:  255        78       0       0

von Wolfgang (Gast)


Lesenswert?

Stefan S. schrieb:
> Habe gerade keine Zeit, dass ganze als Diagramm zu
> posten

Dann poste es wenigstens als Anhang. Wer hat schon Lust, durch den 
ganzen Zahlensalat durchzuscrollen.

Statt wild mit den Datentypen rumzuprobieren, könntest du dir einen Satz 
von Eingangsdaten rauspicken, bei denen es schief geht und dann Schritt 
für Schritt nachvollziehen, warum es schief geht.

von Stefan S. (sschultewolter)


Lesenswert?

Wenn ein Mod die Tabellen rauslöschen kann, wäre es nicht schlecht. Sind 
echt etwas lang ;(

1
void hsv2rgb(struct cRGB *rgb, struct cHSV *hsv, int16_t led)
2
{
3
  // Konvertiert Farbton(h), Saettigung(s), Helligkeit(v) (HSV/HSB) nach RGB
4
  // Dimmen erfolgt ueber Saettigung und Helligkeit fuer eine optisch linieare Ausgabe
5
6
  uint8_t value = hsv[led].v;
7
  uint8_t saturation = hsv[led].s;
8
  uint8_t invsat = 255 - saturation;
9
10
  // Minimale Wert von R, G und B
11
  uint8_t brightness_floor = (value * invsat) / 256;
12
13
  // Maximale Werte von R, G und B
14
  uint8_t color_amplitude = value - brightness_floor;
15
16
  // Bestimme den Abschnitt und berechnet den Versatz
17
  uint8_t section = hsv[led].h / 0x40;  // 0..2
18
  uint8_t offset = hsv[led].h % 0x40;    // 0..63
19
20
  uint8_t rampup = offset;        // 0..63
21
  uint8_t rampdown = (0x40 - 1) - offset;  // 63..0
22
23
  uint8_t rampup_amp_adj = (rampup   * color_amplitude) / (256 / 4);
24
  uint8_t rampdown_amp_adj = (rampdown * color_amplitude) / (256 / 4);
25
26
  uint8_t rampup_adj_with_floor = rampup_amp_adj + brightness_floor;
27
  uint8_t rampdown_adj_with_floor = rampdown_amp_adj + brightness_floor;
28
29
30
  if(section)
31
  {
32
    if(section == 1)
33
    {
34
      // 64..127
35
      rgb[led].r = brightness_floor;
36
      rgb[led].g = rampdown_adj_with_floor;
37
      rgb[led].b = rampup_adj_with_floor;
38
    }
39
    else  // 128..191
40
    {
41
      rgb[led].r = rampup_adj_with_floor;
42
      rgb[led].g = brightness_floor;
43
      rgb[led].b = rampdown_adj_with_floor;
44
    }
45
  }
46
  else // 0..63
47
  {
48
    rgb[led].r = rampdown_adj_with_floor;
49
    rgb[led].g = rampup_adj_with_floor;
50
    rgb[led].b = brightness_floor;
51
  }
52
}

Hatte ein paar Fehler bei der Berechnung, nun aber taucht das Problem 
ebenfalls auf.

Ab Farbton/Hue 192 läuft die Berechnung falsch. Den Kommentaren aus der 
FastLED Lib ist auch nicht zu entnehmen, was bei dem Fall von 192-255 
geschieht.
1
hue:  188        239      0      11
2
hue:  189        243      0       7
3
hue:  190        247      0       3
4
hue:  191        251      0       0
5
hue:  192         0       0      251
6
hue:  193         3       0      247
7
hue:  194         7       0      243
8
hue:  195        11       0      239
9
hue:  196        15       0      235
10
hue:  197        19       0      231
11
hue:  198        23       0      227
12
hue:  199        27       0      223
13
hue:  200        31       0      219

von Mw E. (Firma: fritzler-avr.de) (fritzler)


Lesenswert?

Nimmste den Code hier, der funzt:
http://www.zabex.de/site/sofabeleuchtung.html

von Wolfgang (Gast)


Angehängte Dateien:

Lesenswert?

Stefan S. schrieb:
> Ich sehe gerade, das die Berechnungen so nicht funktionieren. Das ergibt
> keine ordentliche RGB Kurve.
So schlecht sehen die Daten doch gar nicht aus, bis auf die Tatsache, 
dass ein 16Bit Variablentyp erforderlich ist. Die Berechnungen mit int 
liefert doch einen stetigen Verlauf mit Werte 0..334. Bei den anderen 
beiden Berechnung kommt dann nach 255 der Überlauf und es geht wieder 
unten los.

von Stefan S. (sschultewolter)


Lesenswert?

Die 334 kamen von einer falschen Einstellung. Habe da mit 0x55 gerechnet 
anstatt 0x40. Das Problem ist aber, dass zwischen 191 und 192 blau von 0 
auf 254 (? hab genauen Wert gerade nicht im Kopf) springt. Meiner 
Meinung nach funktioniert das ganze nicht von 0-255 sondern von 0-191

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.