-
-
Notifications
You must be signed in to change notification settings - Fork 193
Description
Issue №1126 opened by charlesej at 2019-06-30 15:58:28
The gfxdraw functions do not support surface mapped colors as stated in the documentation.
The color argument can also be an integer pixel value that is already mapped to the Surface's pixel format.
The following code gives an example of a mapped color value being rejected by a gfxdraw function.
# File: gfxdraw_color.py
import pygame
import pygame.gfxdraw
pygame.display.init()
screen = pygame.display.set_mode((150, 150))
RED = pygame.Color('red')
rect = pygame.Rect((0, 0), (40, 40))
for color in ((255, 0, 0), (255, 0, 0, 255), RED, screen.map_rgb(RED)):
print('Drawing using color:', type(color), color)
pygame.draw.rect(screen, color, rect, 1)
pygame.gfxdraw.rectangle(screen, rect, color)
pygame.display.quit()
The above code gives the following output. It shows that a mapped color value is accepted by the pygame.draw.rect()
function, but causes the pygame.gfxdraw.rectangle()
to raise a TypeError
.
pygame 2.0.0.dev2 (SDL 2.0.9, python 3.7.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Drawing using color: <class 'tuple'> (255, 0, 0)
Drawing using color: <class 'tuple'> (255, 0, 0, 255)
Drawing using color: <class 'pygame.Color'> (255, 0, 0, 255)
Drawing using color: <class 'int'> 16711680
Traceback (most recent call last):
File "gfxdraw_color.py", line 13, in <module>
pygame.gfxdraw.rectangle(screen, rect, color)
TypeError: invalid color argument
System details:
- os: windows 10 (64bit)
- python: 3.7.2 (64bit)
- pygame: 2.0.0.dev2 (SDL: 2.0.9) at aba2da7
Comments
# # JLkp commented at 2022-01-20 13:57:37
@illume I suggest to close this issue because it is solved.
I realised this is a very old issue but for anyone looking for this.
The gfxdraw.rectangle
function only supports a Color
or tuple (int, int, int, [int]))
as the color value. In order to make your code work you need to add the unmap_rgb()
function which converts the mapped integer value back to a color tuple. Your code would look like this:
import pygame
import pygame.gfxdraw
pygame.display.init()
screen = pygame.display.set_mode((150, 150))
RED = pygame.Color("red")
rect = pygame.Rect((0, 0), (40, 40))
for color in ((255, 0, 0), (255, 0, 0, 255), RED, screen.map_rgb(RED)):
pygame.draw.rect(screen, color, rect, 1)
if type(color) == int:
color = screen.unmap_rgb(color)
pygame.gfxdraw.rectangle(screen, rect, color)
else:
pygame.gfxdraw.rectangle(screen,rect,color)
print('Drawing using color:', type(color), color)
pygame.display.quit()