Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using color in python
#1
I am creating a game and I want to include colour and I am setting variables to their appropriate value, I here 'color' has to be spelt like that. But I get the error message:

GREEN = color('#00FF00')
NameError: name 'color' is not defined

#colours being assigned
GREEN = color('#00FF00')
BLUE = color('#1E90FF')
RED = color('#FF0000')
YELLOW = color('#FFFF00')
BLACK = color('#000000')
WHITE = color('#FFFFFF')
ORANGE = color('#FFA500')
GOLD = color('#FFD700')
PINK = color('#FF69B4')
Any help is appreciated, thanks
Reply
#2
Where is that color function supposed to come from? Your code doesn't have an import statement for it and it isn't a built-in function that's available by default, hence the error.
Reply
#3
After I do that I now get this error:

GREEN = color('#00FF00')
TypeError: 'module' object is not callable
Reply
#4
Given it's a function, it needs to be imported from the relevant module. There isn't a color module in the standard library, so I'm still not sure where you think this function is meant to come from. If you're using a third-party library, then consult its documentation.
Reply
#5
I thought it was used in pygame which is already imported
Reply
#6
Why aren't you showing the whole code then? It wastes people's time if they have to guess. If it's in the pygame module and that's imported, then you need to qualify the function with its module name (i.e. pygame.color) or use from (i.e. from pygame import color), as I already suggested.
Reply
#7
You can do from pygame import Color or you can just call it pygame.Color. Also the reason you get that error is because it's "Color" and not "color"
Reply
#8
FYI. pygame has over 400+ builtin colors.
import pygame
print(pygame.Color("green"))
print(pygame.Color("gold"))
print(pygame.Color("pink"))
99 percent of computer problems exists between chair and keyboard.
Reply
#9
You really don't need that 'color' thing. You could also just do it like that:

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DARKGREY = (40, 40, 40)
LIGHTGREY = (100, 100, 100)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BROWN = (106, 55, 5)
DARKGREEN = (0, 100, 0, 255)
ORANGE = (255,100,10)
Then if you want to use the colour somewhere, e.g.

BGCOLOR = DARKGREEN
screen.fill(BGCOLOR)
That's the way I am doing it in my game.
Reply
#10
I learn to work with what is supplied.
Why take all that time to define colors ? When there is predefine colors.

If you don't like working with strings. You can always wrap it.
import pygame

class PygameColors:
    def __init__(self):
        self.__dict__.update(pygame.color.THECOLORS)

    def add(self, name, r, g, b, a=255):
        if name not in vars(self).keys():
            setattr(self, name, pygame.Color(r, g, b, a))
        else:
            print("Color already exists:", name)

color = PygameColors()

print("white:", color.white)
print("black:", color.black)
print("darkgray:", color.darkgray)
print("darkgrey:", color.darkgrey)
print("gray16:", color.gray16)
print("green:", color.green)
print("red:", color.red)
print("yellow:", color.yellow)
print("brown:", color.brown)
print("darkgreen:", color.darkgreen)
print("orange:", color.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