Student Coder
Student Coder

Reputation: 1

Invalid color argument in pygame when trying to gradient

I'm trying to change each value of RBG from one color to another target color. My window is 700 units in width and I'm trying to draw 350 rectangles, each 2 pixels wide with a very slightly adjusted color.

I'm doing this using a for-loop but there is an invalid color argument error. I'm also sure there is a better method to do this, but I'm trying to see if this would work.

I'm a very beginner to pygame- excuse my lack of knowledge.

red_v = 199
green_v = 229
blue_v = 255

x = 0
y = 0

for i in range(350):

    pygame.draw.rect(screen, (red_v, green_v, blue_v), (x, y, 2, 500))

    red_v -= 3
    green_v -= 2
    blue_v -= 3

    x += 2

I have tried storing my RBG under a variable name like this:

for i in range(350):

    GRADIENT = [red_v, green_v, blue_v]
    pygame.draw.rect(screen, GRADIENT, (x, y, 2, 500))

    red_v -= 3
    green_v -= 2
    blue_v -= 3

Makes no difference. I'm sure this is probably a very simple fix that I cannot see just yet.

Upvotes: 0

Views: 182

Answers (2)

Rabbid76
Rabbid76

Reputation: 210890

You have to make sure that the color channels are always in range [0, 255]. The can't be negativ:

GRADIENT = [max(0, red_v), max(0, green_v), max(0, blue_v)]
pygame.draw.rect(screen, GRADIENT, (x, y, 2, 500))

Upvotes: 1

oFinally
oFinally

Reputation: 115

I think you need to do

pygame.draw.rect(screen, (red_v, green_v, blue_v), pygame.Rect(x1,y1,x2,y2))

Check this out: https://www.geeksforgeeks.org/how-to-draw-rectangle-in-pygame/

Upvotes: 0

Related Questions