File tree 2 files changed +16
-34
lines changed
2 files changed +16
-34
lines changed Original file line number Diff line number Diff line change @@ -29,17 +29,8 @@ def test_get_palette():
29
29
palette , mode = palette_file .getpalette ()
30
30
31
31
# Assert
32
- assert mode == "RGB"
33
-
34
-
35
- def test_palette__has_correct_color_indexes ():
36
- # Arrange
37
- with open ("Tests/images/custom_gimp_palette.gpl" , "rb" ) as fp :
38
- palette_file = GimpPaletteFile (fp )
39
-
40
- palette , mode = palette_file .getpalette ()
41
-
42
- colors_in_test_palette = [
32
+ expected_palette = b""
33
+ for color in (
43
34
(0 , 0 , 0 ),
44
35
(65 , 38 , 30 ),
45
36
(103 , 62 , 49 ),
@@ -48,15 +39,7 @@ def test_palette__has_correct_color_indexes():
48
39
(208 , 127 , 100 ),
49
40
(151 , 144 , 142 ),
50
41
(221 , 207 , 199 ),
51
- ]
52
-
53
- for i , color in enumerate (colors_in_test_palette ):
54
- assert tuple (palette [i * 3 : i * 3 + 3 ]) == color
55
-
56
-
57
- def test_palette_counts_number_of_colors_in_file ():
58
- # Arrange
59
- with open ("Tests/images/custom_gimp_palette.gpl" , "rb" ) as fp :
60
- palette_file = GimpPaletteFile (fp )
61
-
62
- assert palette_file .n_colors == 8
42
+ ):
43
+ expected_palette += bytes (color )
44
+ assert palette == expected_palette
45
+ assert mode == "RGB"
Original file line number Diff line number Diff line change @@ -26,28 +26,27 @@ class GimpPaletteFile:
26
26
27
27
def __init__ (self , fp ):
28
28
29
- palette = bytearray (b"" .join ([o8 (i ) * 3 for i in range (256 )]))
30
-
31
29
if fp .readline ()[:12 ] != b"GIMP Palette" :
32
30
raise SyntaxError ("not a GIMP palette file" )
33
31
34
- index = 0
35
- for s in fp :
32
+ self .palette = b""
33
+ while len (self .palette ) < 768 :
34
+
35
+ s = fp .readline ()
36
+ if not s :
37
+ break
38
+
36
39
# skip fields and comment lines
37
40
if re .match (rb"\w+:|#" , s ):
38
41
continue
39
42
if len (s ) > 100 :
40
43
raise SyntaxError ("bad palette file" )
41
44
42
- v = tuple ( map ( int , s .split ()[: 3 ]) )
45
+ v = s .split ()
43
46
if len (v ) < 3 :
44
47
raise ValueError ("bad palette entry" )
45
-
46
- palette [index * 3 : index * 3 + 3 ] = v
47
- index += 1
48
-
49
- self .palette = bytes (palette )
50
- self .n_colors = index
48
+ for i in range (3 ):
49
+ self .palette += o8 (int (v [i ]))
51
50
52
51
def getpalette (self ):
53
52
You can’t perform that action at this time.
0 commit comments