Debian Codegraph @

src/cdogs/color.c

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4
5 Copyright (c) 2013-2018, 2024 Cong Xu
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28 */
29 #include "color.h"
30
31 #include <math.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "utils.h"
36
37 color_t colorWhite = {255, 255, 255, 255};
38 color_t colorRed = {255, 0, 0, 255};
39 color_t colorGreen = {0, 255, 0, 255};
40 color_t colorBlue = {0, 0, 255, 255};
41 color_t colorPoison = {64, 192, 64, 255};
42 color_t colorBlack = {0, 0, 0, 255};
43 color_t colorDarker = {192, 192, 192, 255};
44 color_t colorPurple = {192, 0, 192, 255};
45 color_t colorGray = {128, 128, 128, 255};
46 color_t colorDarkGray = {64, 64, 64, 255};
47 color_t colorYellow = {255, 255, 128, 255};
48 color_t colorMagenta = {255, 0, 255, 255};
49 color_t colorCyan = {0, 255, 255, 255};
50 color_t colorFog = {96, 96, 96, 255};
51 color_t colorOrange = {255, 128, 0, 255};
52 color_t colorTransparent = {0, 0, 0, 0};
53
54 color_t colorMaroon = {0x84, 0, 0, 255};
55 color_t colorLonestar = {0x70, 0, 0, 255};
56 color_t colorRusticRed = {0x48, 0, 0, 255};
57 color_t colorOfficeGreen = {0, 0x84, 0, 255};
58 color_t colorPakistanGreen = {0, 0x70, 0, 255};
59 color_t colorDarkFern = {0, 0x48, 0, 255};
60 color_t colorNavyBlue = {0, 0, 0x84, 255};
61 color_t colorArapawa = {0, 0, 0x70, 255};
62 color_t colorStratos = {0, 0, 0x48, 255};
63 color_t colorPatriarch = {0x84, 0, 0x84, 255};
64 color_t colorPompadour = {0x70, 0, 0x70, 255};
65 color_t colorLoulou = {0x48, 0, 0x48, 255};
66 color_t colorBattleshipGrey = {0x84, 0x84, 0x84, 255};
67 color_t colorDoveGray = {0x70, 0x70, 0x70, 255};
68 color_t colorGravel = {0x48, 0x48, 0x48, 255};
69 color_t colorComet = {0x5C, 0x5C, 0x84, 255};
70 color_t colorFiord = {0x48, 0x48, 0x70, 255};
71 color_t colorTuna = {0x34, 0x34, 0x48, 255};
72 color_t colorHacienda = {0x94, 0x80, 0x2C, 255};
73 color_t colorKumera = {0x84, 0x70, 0x24, 255};
74 color_t colorHimalaya = {0x74, 0x60, 0x1C, 255};
75 color_t colorChocolate = {0x84, 0x44, 0, 255};
76 color_t colorNutmeg = {0x70, 0x38, 0, 255};
77 color_t colorBracken = {0x48, 0x24, 0, 255};
78 color_t colorTeal = {0, 0x84, 0x84, 255};
79 color_t colorSkobeloff = {0, 0x70, 0x70, 255};
80 color_t colorDeepJungleGreen = {0, 0x48, 0x48, 255};
81
82 color_t colorSkin = {0xF4, 0x94, 0x4C, 255};
83 color_t colorDarkSkin = {0x93, 0x5D, 0x37, 255};
84 color_t colorAsianSkin = {0xFF, 0xCC, 0x99, 255};
85
86 color_t colorLightBlue = {80, 80, 160, 255};
87 color_t colorSelectedBG = {0, 255, 255, 64};
88
89 color_t ColorMult(color_t c, color_t m)
90 {
91 c.r = (uint8_t)((int)c.r * m.r / 255);
92 c.g = (uint8_t)((int)c.g * m.g / 255);
93 c.b = (uint8_t)((int)c.b * m.b / 255);
94 c.a = (uint8_t)((int)c.a * m.a / 255);
95 return c;
96 }
97 color_t ColorAlphaBlend(color_t a, color_t b)
98 {
99 a.r = (uint8_t)(((int)a.r * (255 - b.a) + (int)b.r * b.a) / 255);
100 a.g = (uint8_t)(((int)a.g * (255 - b.a) + (int)b.g * b.a) / 255);
101 a.b = (uint8_t)(((int)a.b * (255 - b.a) + (int)b.b * b.a) / 255);
102 a.a = 255;
103 return a;
104 }
105
106 HSV tintNone = {-1.0, 1.0, 1.0};
107 HSV tintRed = {0.0, 1.0, 1.0};
108 HSV tintYellow = {60.0, 1.0, 1.0};
109 HSV tintGreen = {120.0, 1.0, 1.0};
110 HSV tintCyan = {180.0, 1.0, 1.0};
111 HSV tintPoison = {120.0, 0.33, 2.0};
112 HSV tintGray = {-1.0, 0.0, 1.0};
113 HSV tintPurple = {300, 1.0, 1.0};
114 HSV tintDarker = {-1.0, 1.0, 0.75};
115
116 color_t ColorTint(color_t c, HSV hsv)
117 {
118 // Adapted from answer by David H
119 // http://stackoverflow.com/a/6930407/2038264
120 // License: http://creativecommons.org/licenses/by-sa/3.0/
121 // Author profile: http://stackoverflow.com/users/1633251/david-h
122 color_t out;
123 int vAvg = ((int)c.r + c.g + c.b) / 3;
124 uint8_t vComponent = (uint8_t)CLAMP(hsv.v * vAvg, 0, 255);
125
126 if (hsv.s <= 0.0)
127 {
128 // No saturation; just gray
129 out.r = vComponent;
130 out.g = vComponent;
131 out.b = vComponent;
132 }
133 else if (hsv.h >= 0)
134 {
135 // set hue to h; use regular HSV to RGB conversion
136 double ff;
137 uint8_t p, q, t;
138 long i;
139 double hh = hsv.h;
140 if (hh >= 360.0)
141 {
142 hh = 0.0;
143 }
144 hh /= 60.0;
145 i = (long)hh;
146 ff = hh - i;
147 p = (uint8_t)CLAMP(vComponent * (1.0 - hsv.s), 0, 255);
148 q = (uint8_t)CLAMP(vComponent * (1.0 - (hsv.s * ff)), 0, 255);
149 t = (uint8_t)CLAMP(vComponent * (1.0 - (hsv.s * (1.0 - ff))), 0, 255);
150
151 switch (i)
152 {
153 case 0:
154 out.r = vComponent;
155 out.g = t;
156 out.b = p;
157 break;
158 case 1:
159 out.r = q;
160 out.g = vComponent;
161 out.b = p;
162 break;
163 case 2:
164 out.r = p;
165 out.g = vComponent;
166 out.b = t;
167 break;
168
169 case 3:
170 out.r = p;
171 out.g = q;
172 out.b = vComponent;
173 break;
174 case 4:
175 out.r = t;
176 out.g = p;
177 out.b = vComponent;
178 break;
179 case 5:
180 default:
181 out.r = vComponent;
182 out.g = p;
183 out.b = q;
184 break;
185 }
186 }
187 else
188 {
189 // Just set saturation and value
190 // Use weighted average to shift components towards grey for saturation
191 out.r = (uint8_t)CLAMP(
192 hsv.v * (vAvg * (1.0 - hsv.s) + hsv.s * c.r), 0, 255);
193 out.g = (uint8_t)CLAMP(
194 hsv.v * (vAvg * (1.0 - hsv.s) + hsv.s * c.g), 0, 255);
195 out.b = (uint8_t)CLAMP(
196 hsv.v * (vAvg * (1.0 - hsv.s) + hsv.s * c.b), 0, 255);
197 }
198 out.a = c.a;
199 return out;
200 }
201
202 bool ColorEquals(const color_t a, const color_t b)
203 {
204 return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
205 }
206 bool HSVEquals(const HSV a, const HSV b)
207 {
208 const double epsilon = 0.000001;
209 return fabs(a.h - b.h) < epsilon && fabs(a.s - b.s) < epsilon &&
210 fabs(a.v - b.v) < epsilon;
211 }
212
213 color_t StrColor(const char *s)
214 {
215 if (s == NULL)
216 {
217 return colorBlack;
218 }
219 const int len = (int)strlen(s);
220 if (len != 6 && len != 8)
221 {
222 return colorBlack;
223 }
224 long long hex = strtoll(s, NULL, 16);
225 color_t c;
226 if (len == 6)
227 {
228 c.a = 255;
229 }
230 else
231 {
232 c.a = hex & 255;
233 hex >>= 8;
234 }
235 c.r = (hex >> 16) & 255;
236 c.g = (hex >> 8) & 255;
237 c.b = hex & 255;
238 return c;
239 }
240 void ColorStr(char *s, const color_t c)
241 {
242 sprintf(s, "%02x%02x%02x%02x", c.r, c.g, c.b, c.a);
243 }