1
votes

I am trying to make a rainbow of rectangles with pygame but I get a problem saying "ValueError: invalid color argument"

import pygame
pygame.init()

width = 400
height = 400
window_size = (width , height)
screen = pygame.display.set_mode(window_size)

colour = pygame.color.Color('#646400')

row = 0
done = False
while not done:
    increment = 255 / 100
    while row <= height:
        pygame.draw.rect(screen, colour, (0, row, width, row + increment))
        pygame.display.flip()
        if colour[2] + increment < 255:
            colour[2] += increment
        row += increment

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
pygame.quit()
3

3 Answers

1
votes

Simplest way is to just use a tuple of rgb values for the color:

color = (100, 100, 0)  # equal to #646400

And then just pass it as the second argument to draw.rect() like you've already done:

pygame.draw.rect(screen, colour, (0, row, width, row + increment))
0
votes

you need to change this, colour = pygame.color.Color('#646400') because it will be easier to go with the RGB colors in a list. change it to this:

colour = [0,0,100] 

or to any kind of color you want maybe mix them to get the rainbow as result. This is what I would do. The result of the code is a gradient blue color. But as I mentioned before you can change it. But remember you cannot use a tuple! Because the aren't mutable.

0
votes

As many others have suggested, PyGame does not support HTML-notation colour strings.

But it's fairly simple to convert these:

# Convert HTML-like colour hex-code to integer triple tuple
# E.g.: "#892da0" -> ( 137, 45, 160 )
def hexToColour( hash_colour ):
    """Convert a HTML-hexadecimal colour string to an RGB triple-tuple"""
    red   = int( hash_colour[1:3], 16 )
    green = int( hash_colour[3:5], 16 )
    blue  = int( hash_colour[5:7], 16 )
    return ( red, green, blue )

...

colour = hexToColour( '#646400' )

It's not normally necessary to convert these into a Color object to use them.