Python Forum
How do you set the alpha of a color???
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you set the alpha of a color???
#1
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.
Reply
#2
How are you writing it? Of the top of my head, I think the format is
color = ((0, 0, 0), 0)
Reply
#3
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()
Reply
#4
I looked at one of my programs. Try double brackets.
((255,0,0,128))
Then it enters as one value instead of 4.
Reply
#5
I didn't get an error message this time, but it still didn't change anything.
Reply
#6
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))
Reply
#7
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.
Reply
#8
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)
99 percent of computer problems exists between chair and keyboard.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020