-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolorscale.cpp
More file actions
107 lines (92 loc) · 2.28 KB
/
colorscale.cpp
File metadata and controls
107 lines (92 loc) · 2.28 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
/*
Scoreview (R)
Copyright (C) 2015 Patrick Areny
All Rights Reserved.
Scoreview is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
float Bx(float s)
{
float l = 1. / 8.;
float a = 8. / 2.;
float a1 = 8.;
if (s >= 0. && s <= l)
return (0.0 + s * a1); // Changs from mathlab color function starts at 0 pitch black.
if (s > l && s <= 2. * l)
return (1.0);
if (s > 2. * l && s < 3. * l)
return (1. - (s - 2. * l) * a);
if (s > 1.5)
return 1.;
return 0.;
}
float Gx(float s)
{
float l = 1. / 8.;
float a = 8. / 2.;
if (s >= l && s <= 3. * l)
return ((s - l) * a);
if (s > 3. * l && s <= 5. * l)
return (1.0);
if (s > 5. * l && s < 7. * l)
return (1. - (s - 5. * l) * a);
if (s > 1.5)
return 1.;
return 0.;
}
float Rx(float s)
{
float l = 1. / 8.;
float a = 8. / 2.;
if (s >= 3. * l && s <= 5. * l)
return ((s - 3. * l) * a);
if (s > 5. * l && s <= 7. * l)
return (1.0);
if (s > 7. * l && s < 8. * l)
return (1. - (s - 7. * l) * a);
if (s > 1.5)
return 1.;
return 0.;
}
// Normalises a value v in 0. and 1. and converts that to a rgb 565 short int
int value_to_color_565(float v, float max)
{
int s;
int R;
int G;
int B;
float fact = max;
v = v / fact;
B = (int)31. * Bx(v);
G = (int)63. * Gx(v);
R = (int)31. * Rx(v);
s = (R % 32) + ((G % 64) << 5) + ((B % 32) << 11);
// if (v == 1.)
// s = 0xFE00;
//printf("vla=%f, max=%f\n", v, max);
return s;
}
// ABGR
int value_to_color_888(float v, float max)
{
int s;
int R;
int G;
int B;
float fact = max;
v = v / fact;
B = (int)255. * Bx(v);
G = (int)255. * Gx(v);
R = (int)255. * Rx(v);
s = (R & 0xFF) + ((G & 0xFF) << 8) + ((B & 0xFF) << 16);
return s;
}