Python Forum
Using color in python - 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: Using color in python (/thread-21110.html)

Pages: 1 2


Using color in python - Help_me_Please - Sep-14-2019

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


RE: Using color in python - ndc85430 - Sep-14-2019

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.


RE: Using color in python - Help_me_Please - Sep-14-2019

After I do that I now get this error:

GREEN = color('#00FF00')
TypeError: 'module' object is not callable


RE: Using color in python - ndc85430 - Sep-14-2019

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.


RE: Using color in python - Help_me_Please - Sep-14-2019

I thought it was used in pygame which is already imported


RE: Using color in python - ndc85430 - Sep-14-2019

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.


RE: Using color in python - SheeppOSU - Sep-14-2019

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"


RE: Using color in python - Windspar - Sep-15-2019

FYI. pygame has over 400+ builtin colors.
import pygame
print(pygame.Color("green"))
print(pygame.Color("gold"))
print(pygame.Color("pink"))



RE: Using color in python - Piethon - Sep-22-2019

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.


RE: Using color in python - Windspar - Sep-22-2019

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)