Basics of colors
Basics of Colors
While going through all kinds of pictures online, have you ever questioned how those pictures get their colors? Actually, computers have RGB mode to represent colors.
In Python, we follow a specific format when defining colors: (R, G, B), where “R”, “G”, and “B” are all integers. “R” represents some amount of red, “G” represents some amount of green, and “B” represents some amount of blue.
The color represented by (R, G, B) is what we get from “the mix of” different amounts of red, green and blue.
What do we mean by the “mix of colors”?
Here we are using different amounts of red, green and blue light to get a new color of light. Just like we use different amounts of pigments while painting, we are mixing different amounts of lights to create different colors of light. Pigments use the primary colors of red, yellow and blue, whereas computers use primary colors of red, green and blue light.
This picture represents very approximately how colors mix up:
Just as we should not use too much pigments, there is also limitation for represent colors. The maximum integer we can use to represent each amount of color is 255 and the minimum integer we can use to represent each amount of color is 0. This is defined in RGB mode.
In all the following materials, “color” means “the color of the light”.
- Which color does (0, 0, 0) represent?
Black. All colors are of 0%. (There is no color here. The whole world is so dark. It is black!)
- Which color does (255, 255, 255) represent?
White. 255 represents that you are using 100% of each color, which is saturate. (When all colors are saturate, you will get white)
- Which color does (100, 100, 100) represent?
Gray. 100 / 255 % = 39.2%. You will get gray by adding up 39.2% of red, 39.2% of blue, and 39.2% of green.
Note: Don’t panic if you feel confused here, we will explain more about these ideas in python code activities later in the workshop.
In Python, most commonly used colors are prepared for you. When you need to refer to a color, just do:
color = 'color name'
For example: Red:
color = 'red' # The following code is used to show you the color you get. # You can skip these lines for now and we will explain them in the next page! img = Image.new('RGB', (60, 30), color) img.save('red.png') img.show('red.png')
The following is your output: Amazing! You get red!