@@ -950,6 +950,37 @@ def test_normalize__all_elements_within_limits(self):
950950 self .assertTrue (0 <= b <= 1 )
951951 self .assertTrue (0 <= a <= 1 )
952952
953+ def test_hex_property (self ):
954+ color = pygame .Color (255 , 0 , 255 , 0 )
955+ hex = color .hex
956+ self .assertEqual (hex , "#ff00ff00" )
957+
958+ for c in rgba_combos_Color_generator ():
959+ col_hex = c .hex
960+ self .assertIsInstance (col_hex , str )
961+ self .assertEqual (len (col_hex ), 9 )
962+ self .assertEqual (col_hex [0 ], "#" )
963+ for char in col_hex :
964+ self .assertIn (char , "#0123456789abcdef" )
965+ self .assertEqual (c , pygame .Color (col_hex ))
966+
967+ with self .assertRaises (TypeError ):
968+ color .hex = 0xFFFFFFFF
969+ with self .assertRaises (AttributeError ):
970+ del color .hex
971+
972+ for value in ["FFFFFFFF" , "#FFzzFF00" , "0x FFFFFF" , "#FF" ]:
973+ for v in [value , value .lower ()]:
974+ with self .assertRaises (ValueError ):
975+ color .hex = v
976+
977+ for value in ["#FFFFFFFF" , "#FFFFFF" , "0xFFFFFFFF" , "0xFFFFFF" ]:
978+ for v in [value , value .lower ()]:
979+ color .hex = v
980+ self .assertEqual (
981+ (color .r , color .g , color .b , color .a ), (255 , 255 , 255 , 255 )
982+ )
983+
953984 def test_issue_284 (self ):
954985 """PyColor OverflowError on HSVA with hue value of 360
955986
@@ -1007,6 +1038,9 @@ def test_i1i2i3__sanity_testing_converted_should_not_raise(self):
10071038 def test_normalized__sanity_testing_converted_should_not_raise (self ):
10081039 self .colorspaces_converted_should_not_raise ("normalized" )
10091040
1041+ def test_hex__sanity_testing_converted_should_not_raise (self ):
1042+ self .colorspaces_converted_should_not_raise ("hex" )
1043+
10101044 ################################################################################
10111045
10121046 def colorspaces_converted_should_equate_bar_rounding (self , prop ):
@@ -1021,7 +1055,7 @@ def colorspaces_converted_should_equate_bar_rounding(self, prop):
10211055 self .assertTrue (abs (other .b - c .b ) <= 1 )
10221056 self .assertTrue (abs (other .g - c .g ) <= 1 )
10231057 # CMY and I1I2I3 do not care about the alpha
1024- if not prop in ("cmy" , "i1i2i3" ):
1058+ if prop not in ("cmy" , "i1i2i3" ):
10251059 self .assertTrue (abs (other .a - c .a ) <= 1 )
10261060
10271061 except ValueError :
@@ -1042,6 +1076,9 @@ def test_i1i2i3__sanity_testing_converted_should_equate_bar_rounding(self):
10421076 def test_normalized__sanity_testing_converted_should_equate_bar_rounding (self ):
10431077 self .colorspaces_converted_should_equate_bar_rounding ("normalized" )
10441078
1079+ def test_hex__sanity_testing_converted_should_equate_bar_rounding (self ):
1080+ self .colorspaces_converted_should_equate_bar_rounding ("hex" )
1081+
10451082 def test_colorspaces_deprecated_large_sequence (self ):
10461083 c = pygame .Color ("black" )
10471084 for space in ("hsla" , "hsva" , "i1i2i3" , "cmy" , "normalized" ):
0 commit comments