forked from ambilight-4-mediaportal/AtmoWin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtmoCalculations.cpp
More file actions
688 lines (577 loc) · 20.1 KB
/
Copy pathAtmoCalculations.cpp
File metadata and controls
688 lines (577 loc) · 20.1 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
* calculations.c: calculations needed by the input devices
*
* See the README file for copyright information and how to reach the author.
*
* $Id: dd59374e429c24226713fd86f84a32b93021ecca $
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "AtmoDefs.h"
#if defined(WIN32)
# include <windows.h>
#endif
#include "AtmoCalculations.h"
#include "AtmoConfig.h"
#include "AtmoDynData.h"
#include "AtmoZoneDefinition.h"
// set accuracy of color calculation
#define h_MAX 255
#define s_MAX 255
#define v_MAX 255
// saturation
#define REDCONSTANT 0.299
#define GREENCONSTANT 0.587
#define BLUECONSTANT 0.114
#define LUMALIMIT 219.0/255.0
#define LUMAVAL 16
// macro
#define POS_DIV(a, b) ( (a)/(b) + ( ((a)%(b) >= (b)/2 ) ? 1 : 0) )
CAtmoColorCalculator::CAtmoColorCalculator(CAtmoConfig *pAtmoConfig)
{
m_pAtmoConfig = pAtmoConfig;
m_Weight = NULL;
m_hue_hist = NULL;
m_windowed_hue_hist = NULL;
m_most_used_hue_last = NULL;
m_most_used_hue = NULL;
m_sat_hist = NULL;
m_windowed_sat_hist = NULL;
m_most_used_sat = NULL;
m_Zone_Weights = NULL;
m_average_v = NULL;
m_average_counter = NULL;
m_LastEdgeWeighting = -1;
m_LastWidescreenMode = -1;
m_LastLayout_TopCount = -1;
m_LastLayout_BottomCount = -1;
m_LastLayout_LRCount = -1;
m_LastNumZones = -1;
}
CAtmoColorCalculator::~CAtmoColorCalculator(void)
{
delete[] m_Weight;
delete[] m_hue_hist;
delete[] m_windowed_hue_hist;
delete[] m_most_used_hue_last;
delete[] m_most_used_hue;
delete[] m_sat_hist;
delete[] m_windowed_sat_hist;
delete[] m_most_used_sat;
delete[] m_Zone_Weights;
delete[] m_average_v;
delete[] m_average_counter;
}
void CAtmoColorCalculator::UpdateParameters()
{
// Zonen Definition neu laden
// diverse Vorberechnungen neu ausführen
// Speicherbuffer neu allokieren!
}
void CAtmoColorCalculator::FindMostUsed(int AtmoSetup_NumZones,int *most_used,long int *windowed_hist)
{
memset(most_used, 0, sizeof(int) * AtmoSetup_NumZones);
for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
{
int value = 0;
// walk trough histogram
for (int i = 0; i < s_MAX+1; i++) // assume s_MAX = h_MAX = v_Max
{
// if new value bigger then old one
int tmp = *windowed_hist; // windowed_hist[zone * (s_MAX+1) + i];
// if (w_sat_hist[channel][i] > value)
if (tmp > value)
{
// remember index
most_used[zone] = i;
// and value
value = tmp;
}
windowed_hist++;
}
}
}
pColorPacket CAtmoColorCalculator::AnalyzeHSV(tHSVColor *HSV_Img)
{
int i; // counter
int AtmoSetup_EdgeWeighting = m_pAtmoConfig->getLiveView_EdgeWeighting();
int AtmoSetup_WidescreenMode = m_pAtmoConfig->getLiveView_WidescreenMode();
unsigned int AtmoSetup_DarknessLimit = m_pAtmoConfig->getLiveView_DarknessLimit();
int AtmoSetup_BrightCorrect = m_pAtmoConfig->getLiveView_BrightCorrect();
int AtmoSetup_SatWinSize = m_pAtmoConfig->getLiveView_SatWinSize();
int AtmoSetup_Overlap = m_pAtmoConfig->getLiveView_Overlap();
int AtmoSetup_NumZones = m_pAtmoConfig->getZoneCount();
tHSVColor *temp_Img;
if(AtmoSetup_NumZones != m_LastNumZones)
{
delete[] m_Weight;
delete[] m_hue_hist;
delete[] m_windowed_hue_hist;
delete[] m_most_used_hue_last;
delete[] m_most_used_hue;
delete[] m_sat_hist;
delete[] m_windowed_sat_hist;
delete[] m_most_used_sat;
delete[] m_Zone_Weights;
delete[] m_average_v;
delete[] m_average_counter;
m_Weight = new int[AtmoSetup_NumZones * IMAGE_SIZE];
m_Zone_Weights = new int*[AtmoSetup_NumZones];
for(int i = 0; i < AtmoSetup_NumZones; i++)
m_Zone_Weights[i] = &m_Weight[i * IMAGE_SIZE];
m_hue_hist = new long int[(h_MAX+1) * AtmoSetup_NumZones];
m_windowed_hue_hist = new long int[(h_MAX+1) * AtmoSetup_NumZones];
m_most_used_hue_last = new int[AtmoSetup_NumZones];
m_most_used_hue = new int[AtmoSetup_NumZones];
memset( m_most_used_hue_last, 0, sizeof(int) * AtmoSetup_NumZones);
m_sat_hist = new long int[(s_MAX+1) * AtmoSetup_NumZones];
m_windowed_sat_hist = new long int[(s_MAX+1) * AtmoSetup_NumZones];
m_most_used_sat = new int[AtmoSetup_NumZones];
m_average_v = new long int[AtmoSetup_NumZones];
m_average_counter = new int[AtmoSetup_NumZones];
m_LastNumZones = AtmoSetup_NumZones;
}
// calculate only if setup has changed
if ((AtmoSetup_EdgeWeighting != m_LastEdgeWeighting) ||
(AtmoSetup_WidescreenMode != m_LastWidescreenMode) ||
(m_pAtmoConfig->getZonesTopCount() != m_LastLayout_TopCount) ||
(m_pAtmoConfig->getZonesBottomCount() != m_LastLayout_BottomCount) ||
(m_pAtmoConfig->getZonesLRCount() != m_LastLayout_LRCount) ||
(m_pAtmoConfig->m_UpdateEdgeWeightningFlag != 0)
)
{
for(i = 0 ;i < AtmoSetup_NumZones; i++) {
CAtmoZoneDefinition *pZoneDef = m_pAtmoConfig->getZoneDefinition(i);
if(pZoneDef)
{
pZoneDef->UpdateWeighting(m_Zone_Weights[i],
AtmoSetup_WidescreenMode,
AtmoSetup_EdgeWeighting);
#ifdef _debug_zone_weight_
char filename[128];
sprintf(filename, "zone_%d_gradient_debug.bmp",i);
pZoneDef->SaveZoneBitmap( filename );
sprintf(filename, "zone_%d_weight_%d_debug.bmp",i,AtmoSetup_EdgeWeighting);
pZoneDef->SaveWeightBitmap(filename, m_Zone_Weights[i] );
#endif
}
}
m_pAtmoConfig->m_UpdateEdgeWeightningFlag = 0;
m_LastEdgeWeighting = AtmoSetup_EdgeWeighting;
m_LastWidescreenMode = AtmoSetup_WidescreenMode;
m_LastLayout_TopCount = m_pAtmoConfig->getZonesTopCount();
m_LastLayout_BottomCount = m_pAtmoConfig->getZonesBottomCount();
m_LastLayout_LRCount = m_pAtmoConfig->getZonesLRCount();
}
AtmoSetup_DarknessLimit = AtmoSetup_DarknessLimit * 10;
/***************************************************************************/
/* Hue */
/***************************************************************************/
/*----------------------------*/
/* hue histogram builtup */
/*----------------------------*/
// HSV histogram
// long int hue_hist[CAP_MAX_NUM_ZONES][h_MAX+1];
// average brightness (value)
// m_average_v m_average_counter
// clean histogram --> calloc
memset(m_hue_hist, 0, sizeof(long int) * (h_MAX+1) * AtmoSetup_NumZones);
memset(m_average_v, 0, sizeof(long int) * AtmoSetup_NumZones);
memset(m_average_counter, 0, sizeof(int) * AtmoSetup_NumZones);
temp_Img = HSV_Img;
i = 0;
for (int row = 0; row < CAP_HEIGHT; row++)
{
for (int column = 0; column < CAP_WIDTH; column++)
{
// forget black bars: perform calculations only if pixel has some luminosity
if ((*temp_Img).v > AtmoSetup_DarknessLimit)
{
// builtup histogram for the x Zones of the Display
for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
{
// Add weight to channel
// Weight(zone, pixel_nummer) m_Weight[((zone) * (IMAGE_SIZE)) + (pixel_nummer)]
// m_hue_hist[zone*(h_MAX+1) + HSV_Img[i].h] += m_Zone_Weights[zone][i] * HSV_Img[i].v;
m_hue_hist[zone*(h_MAX+1) + (*temp_Img).h] += m_Zone_Weights[zone][i] * temp_Img->v;
if(m_Zone_Weights[zone][i] > 0) {
m_average_v[zone] += temp_Img->v;
m_average_counter[zone]++;
}
}
// calculate brightness average
}
temp_Img++;
i++;
}
}
/*----------------------------*/
/* hue histogram windowing */
/*----------------------------*/
// windowed HSV histogram
// long int w_hue_hist[CAP_MAX_NUM_ZONES][h_MAX+1]; -> m_windowed_hue_hist
// clean windowed histogram
memset(m_windowed_hue_hist, 0, sizeof(long int) * (h_MAX+1) * AtmoSetup_NumZones);
// steps in each direction; eg. 2 => -2 -1 0 1 2 windowing
int hue_windowsize = m_pAtmoConfig->getLiveView_HueWinSize();
for (i = 0; i < h_MAX+1; i++) // walk through histogram [0;h_MAX]
{
// windowing from -hue_windowsize -> +hue_windowsize
for (int mywin = -hue_windowsize; mywin < hue_windowsize+1; mywin++)
{
// addressed histogram candlestick
int myidx = i + mywin;
// handle beginning of windowing -> roll back
if (myidx < 0) { myidx = myidx + h_MAX + 1; }
// handle end of windowing -> roll forward
if (myidx > h_MAX) { myidx = myidx - h_MAX - 1; }
// Apply windowing to all x zones
for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
{
// apply lite triangular window design with gradient of 10% per discrete step
m_windowed_hue_hist[(zone * (h_MAX+1)) + i] += m_hue_hist[(zone * (h_MAX+1)) + myidx] * ((hue_windowsize+1)-abs(mywin)); // apply window
}
}
}
/*--------------------------------------*/
/* analyze histogram for most used hue */
/*--------------------------------------*/
// index of last maximum
// static int most_used_hue_last[CAP_MAX_NUM_ZONES] = {0, 0, 0, 0, 0}; --> m_most_used_hue_last
// resulting hue for each channel
//int most_used_hue[CAP_MAX_NUM_ZONES]; --> m_most_used_hue
FindMostUsed(AtmoSetup_NumZones, m_most_used_hue, m_windowed_hue_hist);
for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
{
float percent = (float)m_windowed_hue_hist[zone * (h_MAX+1) + m_most_used_hue_last[zone]] / (float)m_windowed_hue_hist[zone * (h_MAX+1) + m_most_used_hue[zone]];
if (percent > 0.93f) // less than 7% difference?
m_most_used_hue[zone] = m_most_used_hue_last[zone]; // use last index
else
m_most_used_hue_last[zone] = m_most_used_hue[zone];
}
/*
memset(m_most_used_hue, 0, sizeof(int) * AtmoSetup_NumZones);
for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
{
long int value = 0;
for (i = 0; i < h_MAX+1; i++) // walk through histogram
{
long int tmp = m_windowed_hue_hist[ (zone * (h_MAX+1)) + i ];
if (tmp > value) // if new value bigger then old one
{
m_most_used_hue[zone] = i; // remember index
value = tmp; // w_hue_hist[zone][i]; // and value
}
}
float percent = (float)m_windowed_hue_hist[zone * (h_MAX+1) + m_most_used_hue_last[zone]] / (float)value;
if (percent > 0.93f) // less than 7% difference?
{
m_most_used_hue[zone] = m_most_used_hue_last[zone]; // use last index
}
m_most_used_hue_last[zone] = m_most_used_hue[zone]; // save current index of most used hue
}
*/
/***************************************************************************/
/* saturation */
/***************************************************************************/
// sat histogram
// long int sat_hist[CAP_MAX_NUM_ZONES][s_MAX+1]; -> m_sat_hist
// hue of the pixel we are working at
int pixel_hue = 0;
// clean histogram
memset(m_sat_hist, 0, sizeof(long int) * (s_MAX+1) * AtmoSetup_NumZones);
/*--------------------------------------*/
/* saturation histogram builtup */
/*--------------------------------------*/
i = 0;
temp_Img = HSV_Img;
for (int row = 0; row < CAP_HEIGHT; row++)
{
for (int column = 0; column < CAP_WIDTH; column++)
{
// forget black bars: perform calculations only if pixel has some luminosity
if ((*temp_Img).v > AtmoSetup_DarknessLimit)
{
// find histogram position for pixel
pixel_hue = (*temp_Img).h;
// TODO: brightness calculation(if we require it some time)
for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
{
// only use pixel for histogram if hue is near most_used_hue
if ((pixel_hue > m_most_used_hue[zone] - hue_windowsize) &&
(pixel_hue < m_most_used_hue[zone] + hue_windowsize))
{
// build histogram
m_sat_hist[zone * (s_MAX+1) + (*temp_Img).s ] += m_Zone_Weights[zone][i] * (*temp_Img).v;
}
}
}
i++;
temp_Img++;
}
}
/*--------------------------------------*/
/* saturation histogram windowing */
/*--------------------------------------*/
// windowed HSV histogram
// long int w_sat_hist[CAP_MAX_NUM_ZONES][s_MAX+1]; --> m_windowed_sat_hist
// clean windowed histogram
memset(m_windowed_sat_hist, 0, sizeof(long int) * (s_MAX+1) * AtmoSetup_NumZones);
// steps in each direction; eg. 2 => -2 -1 0 1 2 windowing
int sat_windowsize = AtmoSetup_SatWinSize;
// walk through histogram [0;h_MAX]
for (i = 0; i < s_MAX + 1; i++)
{
// windowing from -hue_windowsize -> +hue_windowsize
for (int mywin = -sat_windowsize; mywin < sat_windowsize+1; mywin++)
{
// addressed histogram candlestick
int myidx = i + mywin;
// handle beginning of windowing -> roll back
if (myidx < 0) { myidx = myidx + s_MAX + 1; }
// handle end of windowing -> roll forward
if (myidx > h_MAX) { myidx = myidx - s_MAX - 1; }
for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
{
m_windowed_sat_hist[zone * (s_MAX+1) + i] += m_sat_hist[zone* (h_MAX+1) + myidx] *
((sat_windowsize+1)-abs(mywin)); // apply window
}
}
}
/*--------------------------------------*/
/* analyze histogram for most used sat */
/*--------------------------------------*/
// resulting sat (most_used_hue) for each channel
// int most_used_sat[CAP_MAX_NUM_ZONES];->m_most_used_sat
FindMostUsed(AtmoSetup_NumZones, m_most_used_sat, m_windowed_sat_hist);
/*----------------------------*/
/* adjust and copy results */
/*----------------------------*/
tHSVColor hsv_pixel;
// storage container for resulting RGB values
pColorPacket output_colors;
AllocColorPacket(output_colors, AtmoSetup_NumZones);
// adjust brightness
// int new_value = (int) ((float)value_avg * ((float)AtmoSetup_BrightCorrect / 100.0));
// if (new_value > 255) new_value = 255; // ensure brightness isn't set too high
// hsv_pixel.v = (unsigned char)new_value;
/*
// calculate brightness average
for(int zone = 0; zone < AtmoSetup_NumZones; zone++) {
if(m_average_counter[zone] > 0)
m_average_v[zone] = m_average_v[zone] / m_average_counter[zone]
else
m_average_v[zone] = AtmoSetup_DarknessLimit;
}
*/
for (int zone = 0; zone < AtmoSetup_NumZones; zone++)
{
if(m_average_counter[zone] > 0)
m_average_v[zone] = m_average_v[zone] / m_average_counter[zone];
else
m_average_v[zone] = AtmoSetup_DarknessLimit;
// m_average_v[zone] = 0;
m_average_v[zone] = (int)((float)m_average_v[zone] * ((float)AtmoSetup_BrightCorrect / 100.0));
hsv_pixel.v = (unsigned char)ATMO_MAX(ATMO_MIN(m_average_v[zone],255),0);
hsv_pixel.h = m_most_used_hue[zone];
hsv_pixel.s = m_most_used_sat[zone];
// convert back to rgb
output_colors->zone[zone] = HSV2RGB(hsv_pixel,m_pAtmoConfig->getLiveView_Saturation());
}
return output_colors;
}
float RGB2_S_(tRGBColor color)
{
int min, max, delta;
int r, g, b;
float s;
r = color.r;
g = color.g;
b = color.b;
min = ATMO_MIN(ATMO_MIN(r, g), b);
max = ATMO_MAX(ATMO_MAX(r, g), b);
delta = max - min;
if (delta == 0) // This is a gray, no chroma...
{
// HSV results = 0 / 1
s = 0;
}
else // Chromatic data...
{
s = ((float)delta/ (float)max) ;
}
return s;
}
tHSVColor RGB2HSV(tRGBColor color)
{
// tHSVColor hsv;
// unsigned char rgbMin, rgbMax;
// rgbMin = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b);
// rgbMax = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b);
// hsv.v = rgbMax;
// if (hsv.v == 0)
// {
// hsv.h = 0;
// hsv.s = 0;
// return hsv;
// }
// hsv.s = 255 * long(rgbMax - rgbMin) / hsv.v;
// if (hsv.s == 0)
// {
// hsv.h = 0;
// return hsv;
// }
// if (rgbMax == rgb.r)
// hsv.h = 0 + 43 * (rgb.g - rgb.b) / (rgbMax - rgbMin);
// else if (rgbMax == rgb.g)
// hsv.h = 85 + 43 * (rgb.b - rgb.r) / (rgbMax - rgbMin);
// else
// // hsv.h = (171+15 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin)) < 256 ? (171+15 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin)) : ((171+15 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin))-256);
// hsv.h = (171 + 43 * (rgb.r - rgb.g) / (rgbMax - rgbMin));
//if ( hsv.h < 0 ) { hsv.h += h_MAX; }
// if ( hsv.h > h_MAX ) { hsv.h -= h_MAX; }
// return hsv;
int min, max, delta;
int r, g, b;
int h = 0;
tHSVColor hsv;
r = color.r;
g = color.g;
b = color.b;
min = ATMO_MIN(ATMO_MIN(r, g), b);
max = ATMO_MAX(ATMO_MAX(r, g), b);
delta = max - min;
hsv.v = (unsigned char) POS_DIV( max*v_MAX, 255 );
if (delta == 0) // This is a gray, no chroma...
{
h = 0; // HSV results = 0 / 1
hsv.s = 0;
}
else // Chromatic data...
{
hsv.s = (unsigned char) ((POS_DIV( (delta*s_MAX) , max ))) ;
int dr = (max - r) + 3*delta;
int dg = (max - g) + 3*delta;
int db = (max - b) + 3*delta;
int divisor = 6*delta;
if (r == max)
{
h = POS_DIV(( (db - dg) * h_MAX ) , divisor);
}
else if (g == max)
{
h = POS_DIV( ((dr - db) * h_MAX) , divisor) + (h_MAX/3);
}
else if (b == max)
{
h = POS_DIV(( (dg - dr) * h_MAX) , divisor) + (h_MAX/3)*2 ;
}
if ( h < 0 ) { h += h_MAX; }
if ( h > h_MAX ) { h -= h_MAX; }
}
hsv.h = (unsigned char)h;
return hsv;
}
tRGBColor HSV2RGB(tHSVColor color, int saturation)
{
/* tRGBColor rgb;
unsigned char region, remainder, p, q, t;
if (hsv.s == 0)
{
rgb.r = hsv.v;
rgb.g = hsv.v;
rgb.b = hsv.v;
return rgb;
}
region = hsv.h / 43;
remainder = (hsv.h - (region * 43)) * 6;
p = (hsv.v * (255 - hsv.s)) >> 8;
q = (hsv.v * (255 - ((hsv.s * remainder) >> 8))) >> 8;
t = (hsv.v * (255 - ((hsv.s * (255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
rgb.r = hsv.v; rgb.g = t; rgb.b = p;
break;
case 1:
rgb.r = q; rgb.g = hsv.v; rgb.b = p;
break;
case 2:
rgb.r = p; rgb.g = hsv.v; rgb.b = t;
break;
case 3:
rgb.r = p; rgb.g = q; rgb.b = hsv.v;
break;
case 4:
rgb.r = t; rgb.g = p; rgb.b = hsv.v;
break;
default:
rgb.r = hsv.v; rgb.g = p; rgb.b = q;
break;
}
return rgb;*/
tRGBColor rgb = {0, 0, 0};
float h = (float)(color.h)/(float)h_MAX;
float s = (float)color.s/(float)s_MAX*(float)((float)saturation/100.0f);
if ( s > 1 ) { s = 1; }
float v = (float)color.v/(float)v_MAX;
//float v = (float)color.v/(float)v_MAX*(float)((float)saturation/100.0f);
//if ( v > 1 ) { v = 1; }
if (s == 0)
{
rgb.r = (int)((v*255.0)+0.5);
rgb.g = rgb.r;
rgb.b = rgb.r;
}
else
{
h = h * 6.0f;
if (h == 6.0) { h = 0.0; }
int i = (int)h;
float f = h - i;
float p = v*(1.0f-s);
float q = v*(1.0f-(s*f));
float t = v*(1.0f-(s*(1.0f-f)));
if (i == 0)
{
rgb.r = (int)((v*255.0)+0.5);//nein
rgb.g = (int)((t*255.0)+0.5);
rgb.b = (int)((p*255.0)+0.5);
}
else if (i == 1)
{
rgb.r = (int)((q*255.0)+0.5);//nein
rgb.g = (int)((v*255.0)+0.5);
rgb.b = (int)((p*255.0)+0.5);
}
else if (i == 2)
{
rgb.r = (int)((p*255.0)+0.5);//nein
rgb.g = (int)((v*255.0)+0.5);
rgb.b = (int)((t*255.0)+0.5);
}
else if (i == 3)
{
rgb.r = (int)((p*255.0)+0.5);
rgb.g = (int)((q*255.0)+0.5);
rgb.b = (int)((v*255.0)+0.5);
}
else if (i == 4)
{
rgb.r = (int)((t*255.0)+0.5);//
rgb.g = (int)((p*255.0)+0.5);
rgb.b = (int)((v*255.0)+0.5);
}
else
{
rgb.r = (int)((v*255.0)+0.5);//nein
rgb.g = (int)((p*255.0)+0.5);
rgb.b = (int)((q*255.0)+0.5);
}
}
return rgb;
}