Skip to content

Commit 7ca9d1c

Browse files
author
Antonis
committed
add Color.from_hex
1 parent eaf3aa0 commit 7ca9d1c

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

buildconfig/stubs/pygame/color.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class Color(Collection[int]):
7878
@overload
7979
@classmethod
8080
def from_normalized(cls, r: float, g: float, b: float, a: float, /) -> Color: ...
81+
@overload
82+
@classmethod
83+
def from_hex(cls, hex:str, /) -> Color: ...
8184
def normalize(self) -> tuple[float, float, float, float]: ...
8285
def correct_gamma(self, gamma: float, /) -> Color: ...
8386
@deprecated("since 2.1.3. Use unpacking instead")

docs/reST/ref/color.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@
292292

293293
.. ## Color.from_normalized ##
294294
295+
.. classmethod:: from_hex
296+
297+
| :sl:`Returns a Color object from a Hexadecimal representation`
298+
| :sg:`from_hex(hex, /) -> Color`
299+
300+
Creates a Color object from the given Hexadecimal components. Refer to :attr:`Color.hex`
301+
for more information.
302+
303+
.. versionadded:: 2.5.0
304+
305+
.. ## Color.from_hex ##
306+
295307
.. method:: normalize
296308

297309
| :sl:`Returns the normalized RGBA values of the Color.`

src_c/color.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ COLOR_FROM_SPACE(hsla);
116116
COLOR_FROM_SPACE(cmy);
117117
COLOR_FROM_SPACE(i1i2i3);
118118
COLOR_FROM_SPACE(normalized);
119+
COLOR_FROM_SPACE(hex);
119120
#undef COLOR_FROM_SPACE
120121

121122
/* Getters/setters */
@@ -240,6 +241,8 @@ static PyMethodDef _color_methods[] = {
240241
DOC_COLOR_FROMI1I2I3},
241242
{"from_normalized", (PyCFunction)_color_from_normalized,
242243
METH_CLASS | METH_VARARGS, DOC_COLOR_FROMNORMALIZED},
244+
{"from_hex", (PyCFunction)_color_from_hex,
245+
METH_CLASS | METH_VARARGS, DOC_COLOR_FROMHEX},
243246
{"normalize", (PyCFunction)_color_normalize, METH_NOARGS,
244247
DOC_COLOR_NORMALIZE},
245248
{"correct_gamma", (PyCFunction)_color_correct_gamma, METH_VARARGS,
@@ -723,6 +726,9 @@ _color_from_space(char *space, PyObject *args)
723726
else if (strcmp(space, "normalized") == 0) {
724727
set_success = _color_set_normalized(color, args, NULL);
725728
}
729+
else if (strcmp(space, "hex") == 0) {
730+
set_success = _color_set_hex(color, args, NULL);
731+
}
726732

727733
if (set_success != 0) {
728734
return NULL;

src_c/doc/color_doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define DOC_COLOR_FROMHSLA "from_hsla(object, /) -> Color\nfrom_hsla(h, s, l, a, /) -> Color\nReturns a Color object from an HSLA representation"
1616
#define DOC_COLOR_FROMI1I2I3 "from_i1i2i3(object, /) -> Color\nfrom_i1i2i3(i1, i2, i3, /) -> Color\nReturns a Color object from a I1I2I3 representation"
1717
#define DOC_COLOR_FROMNORMALIZED "from_normalized(object, /) -> Color\nfrom_normalized(r, g, b, a /) -> Color\nReturns a Color object from a Normalized representation"
18+
#define DOC_COLOR_FROMHEX "from_hex(hex, /) -> Color\nReturns a Color object from a Hexadecimal representation"
1819
#define DOC_COLOR_NORMALIZE "normalize() -> tuple\nReturns the normalized RGBA values of the Color."
1920
#define DOC_COLOR_CORRECTGAMMA "correct_gamma(gamma, /) -> Color\nApplies a certain gamma value to the Color."
2021
#define DOC_COLOR_SETLENGTH "set_length(len, /) -> None\nSet the number of elements in the Color to 1,2,3, or 4."

test/color_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,18 @@ def test_from_normalized(self):
848848
ValueError, lambda: pygame.Color.from_normalized(1, 1, 1, 1, "lel")
849849
)
850850

851+
def test_from_hex(self):
852+
hex = pygame.Color.from_hex("#FFFFFF")
853+
854+
expected_hex = (255, 255, 255, 255)
855+
856+
self.assertEqual(expected_hex, hex)
857+
858+
self.assertRaises(
859+
ValueError, lambda: pygame.Color.from_hex("not-a-color")
860+
)
861+
862+
851863
def test_normalize(self):
852864
c = pygame.Color(204, 38, 194, 55)
853865
self.assertEqual(c.r, 204)

0 commit comments

Comments
 (0)