Python Forum
How do you set the alpha of a color??? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: How do you set the alpha of a color??? (/thread-23737.html)



How do you set the alpha of a color??? - xBlackHeartx - Jan-15-2020

When I enter in the alpha value for a color, nothing changes no matter what I set it to. I don't even get an error message. My program just acts like I haven't set a fourth value in the rgba tuple.


RE: How do you set the alpha of a color??? - michael1789 - Jan-15-2020

How are you writing it? Of the top of my head, I think the format is
color = ((0, 0, 0), 0)



RE: How do you set the alpha of a color??? - xBlackHeartx - Jan-15-2020

I entered that, and it told me I had an 'invalid color' error.

This is the code I'm trying to get to work:

import pygame
pygame.init()

block=100
winH=block*5
winW=block*5

screen=pygame.display.set_mode((winW,winH))

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            quit()
            
    ##colors
    pygame.draw.rect(screen,(255,0,0,128),(0,0,block,winH))

    ##shading
    shade1=pygame.draw.rect(screen,(0,255,0,128),(0,0,winW,block))

    pygame.display.update()



RE: How do you set the alpha of a color??? - michael1789 - Jan-15-2020

I looked at one of my programs. Try double brackets.
((255,0,0,128))
Then it enters as one value instead of 4.


RE: How do you set the alpha of a color??? - xBlackHeartx - Jan-15-2020

I didn't get an error message this time, but it still didn't change anything.


RE: How do you set the alpha of a color??? - michael1789 - Jan-15-2020

All the examples I can find use "Surface". This is from Stack overflow:

s = pygame.Surface((1000,750))  # the size of your rect
s.set_alpha(128)                # alpha level
s.fill((255,255,255))           # this fills the entire surface
windowSurface.blit(s, (0,0))    # (0,0) are the top-left coordinates

or,

s = pygame.Surface((1000,750), pygame.SRCALPHA)   # per-pixel alpha
s.fill((255,255,255,128))                         # notice the alpha value in the color
windowSurface.blit(s, (0,0))



RE: How do you set the alpha of a color??? - xBlackHeartx - Jan-15-2020

I was trying to overlay the image onto rectangles I drew with draw.rect. I was trying to find an alternate way to lighten and darken colors that didn't cause colors such as orange to shift to another color. That green box was originally black btw.

Guess I'll re-write the program with this 'surface' thing and see if that gives me what I want.

Well, that works now. Thank you. I thought I was going to have to re-write the entire program, but realized that I could just leave the color rectangles and just change the 'shaders'. Mixing in grey seems to give me odd results, but that's why I'm making such a program. I'm trying to find a color palette I like for my games. So far, haven't had much luck.


RE: How do you set the alpha of a color??? - Windspar - Jan-15-2020

Pygame draw rect does not use alpha. It will accept the color values. But it will always ignore alpha.

You can also darken colors with hsla.
import pygame
orange = pygame.Color("orange")
print(orange)
darken = orange.hsla
darken = darken[0], darken[1], darken[2] - 5, darken[3]
orange.hsla = darken
print(orange)