Skip to content

Commit 2fb4cea

Browse files
committed
More tests
1 parent 9c39a6b commit 2fb4cea

File tree

4 files changed

+1504
-4
lines changed

4 files changed

+1504
-4
lines changed

modules/yup_graphics/graphics/yup_Color.cpp

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,47 @@ int parseNextInt (String::CharPointerType& data)
5757
return isNegative ? -result : result;
5858
}
5959

60+
float parseNextFloat (String::CharPointerType& data)
61+
{
62+
float result = 0;
63+
bool isNegative = false;
64+
65+
while (*data != '\0' && (*data == ' ' || *data == ','))
66+
++data;
67+
68+
if (*data == '-')
69+
++data;
70+
71+
while (*data >= '0' && *data <= '9')
72+
{
73+
result = result * 10 + (*data - '0');
74+
++data;
75+
}
76+
77+
if (*data == '.')
78+
{
79+
++data;
80+
float decimalPart = 0;
81+
float decimalFactor = 10;
82+
83+
while (*data >= '0' && *data <= '9')
84+
{
85+
decimalPart = decimalPart * 10 + (*data - '0');
86+
++data;
87+
}
88+
89+
result += decimalPart / decimalFactor;
90+
}
91+
92+
if (*data == '%')
93+
{
94+
result /= 100.0f;
95+
++data;
96+
}
97+
98+
return isNegative ? -result : result;
99+
}
100+
60101
Color parseHexColor (const String& hexString)
61102
{
62103
const int length = hexString.length();
@@ -89,7 +130,7 @@ Color parseHexColor (const String& hexString)
89130
}
90131
else
91132
{
92-
return {};
133+
return Colors::transparentBlack;
93134
}
94135
}
95136

@@ -102,7 +143,7 @@ Color parseRGBColor (const String& rgbString)
102143
bool isRGB = rgbString.startsWithIgnoreCase ("rgb(");
103144

104145
if (! isRGBA && ! isRGB)
105-
return {};
146+
return Colors::transparentBlack;
106147

107148
while (*data != '(' && *data != '\0')
108149
++data;
@@ -117,15 +158,41 @@ Color parseRGBColor (const String& rgbString)
117158
if (isRGBA)
118159
a = parseNextInt (data);
119160

120-
return { static_cast<uint8> (r), static_cast<uint8> (g), static_cast<uint8> (b), static_cast<uint8> (a) };
161+
return Color::fromRGBA (static_cast<uint8> (r), static_cast<uint8> (g), static_cast<uint8> (b), static_cast<uint8> (a));
162+
}
163+
164+
Color parseHSLColor (const String& hslString)
165+
{
166+
auto data = hslString.getCharPointer();
167+
bool isHSL = hslString.startsWithIgnoreCase ("hsl(");
168+
bool isHSLA = hslString.startsWithIgnoreCase ("hsla(");
169+
170+
if (! isHSL && ! isHSLA)
171+
return Colors::transparentBlack;
172+
173+
while (*data != '(' && *data != '\0')
174+
++data;
175+
176+
if (*data == '(')
177+
++data;
178+
179+
float h = 0, s = 0, l = 0, a = 1;
180+
h = parseNextFloat (data);
181+
s = parseNextFloat (data);
182+
l = parseNextFloat (data);
183+
184+
if (isHSLA)
185+
a = parseNextFloat (data);
186+
187+
return Color::fromHSL (h, s, l, a);
121188
}
122189

123190
Color parseNamedColor (const String& name)
124191
{
125192
if (auto color = Colors::getNamedColor (name))
126193
return *color;
127194

128-
return {};
195+
return Colors::transparentBlack;
129196
}
130197
} // namespace
131198

@@ -161,6 +228,9 @@ Color Color::fromString (const String& colorString)
161228
else if (colorString.startsWithIgnoreCase ("rgb"))
162229
return parseRGBColor (colorString);
163230

231+
else if (colorString.startsWithIgnoreCase ("hsl"))
232+
return parseHSLColor (colorString);
233+
164234
else
165235
return parseNamedColor (colorString);
166236
}

0 commit comments

Comments
 (0)