Skip to content

Commit 1e49d04

Browse files
authored
Merge pull request #3456 from ToniDevStuff/add-Color.from_hex
add Color.from_hex
2 parents 0779af7 + 7142f56 commit 1e49d04

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

buildconfig/stubs/pygame/color.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class Color(Collection[int]):
7878
@overload
7979
@classmethod
8080
def from_normalized(cls, r: float, g: float, b: float, a: float, /) -> Color: ...
81+
@classmethod
82+
def from_hex(cls, hex: str, /) -> Color: ...
8183
def normalize(self) -> tuple[float, float, float, float]: ...
8284
def correct_gamma(self, gamma: float, /) -> Color: ...
8385
@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.6
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, METH_CLASS | METH_VARARGS,
245+
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,29 @@ 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+
color1 = pygame.Color.from_hex("#FFFFFF") # White
853+
color2 = pygame.Color.from_hex("#000000") # Black
854+
color3 = pygame.Color.from_hex("#AAFF00") # Random
855+
color4 = pygame.Color.from_hex("#FF000080") # Red (50% opacity)
856+
857+
expected_color1 = (255, 255, 255, 255)
858+
expected_color2 = (0, 0, 0, 255)
859+
expected_color3 = (170, 255, 0, 255)
860+
expected_color4 = (255, 0, 0, 128)
861+
862+
self.assertEqual(color1, expected_color1)
863+
self.assertEqual(color2, expected_color2)
864+
self.assertEqual(color3, expected_color3)
865+
self.assertEqual(color4, expected_color4)
866+
867+
self.assertRaises(ValueError, lambda: pygame.Color.from_hex("#FFFFFG"))
868+
self.assertRaises(ValueError, lambda: pygame.Color.from_hex("#FFFFFFF"))
869+
self.assertRaises(ValueError, lambda: pygame.Color.from_hex("not-a-color"))
870+
self.assertRaises(
871+
TypeError, lambda: pygame.Color.from_hex("not-a-color", "lel")
872+
)
873+
851874
def test_normalize(self):
852875
c = pygame.Color(204, 38, 194, 55)
853876
self.assertEqual(c.r, 204)

0 commit comments

Comments
 (0)