Color Functions Assignment

This function will use RGB color values. You might want to read about Image Pixel Data and Python Tuples before you continue.

Part A: Grayscale Inputs (“L” or “Luminance” mode)

The first functions will input a single color value.

1. Create a function that inputs an L value as a parameter, which can be from 0 to 255, and returns a value that is lighter by 50 ticks. However, make sure the value is not greater than 255.

The function must have an input parameter and a return value.

Example

def lighten(L):
   # your code goes here
   # your code goes here

# test the function, perhaps like this:
L1 = lighten(10)
print(L1) # should print 60
L2 = lighten(240)
print(L2)  # should print 255

2. Create a function that changes an L value to either full light or full dark, depending on which value the original color is closest to. This could be used to convert an image to binary / black&white,  which means it would only have two possible colors instead of grayscale.

3. Create a function that inputs two L values and returns the average of those two numbers.

4. Create a function that inputs an L value and one other value (a float) and returns a new L value that’s a certain percentage lighter.

Example:

def pct_lighter(L, p):
    # your code here

print(pct_lighter(100, 0.25))
# should return 125

print(pct_lighter(40, 0.75))
# should return 70

print(pct_lighter(230, 0.75))
# should return 255 (max value=255)

Part B: RGB input

The next group of functions will receive a tuple of three values as an input parameter.

5. Create a function that darkens a color.

The input parameter is a color tuple consisting of (r, g, b) numbers from 0 to 255. The output color should contain the same color, but with a darker shade.

6. Invert a color. That is, create the negative of that color.

To calculate the negative of a color, subtract the value from 255. Return the tuple of all three colors as inverted colors.

7. Blend two colors. Receive two (r, g, b) color tuples as two input parameters. Return a new color that contains the average of red values, the average of green values, and the average of blue values.

8. Color detection (this one is more tricky).

Create a function that inputs a tuple of three numbers representing red, green, and blue color values that can range from 0 to 255. Write code that returns a color name string according to the values given. At minimum, make the code able to return red, green, and blue as results. For a better function, also include secondary colors (yellow, cyan, purple), black, white, and gray.

Keep in mind that many different combinations of (r,g,b) numbers can create a given olor, so you will definitely want to use “less than” and “greater than” operators, < >, in your function.

def color(c):
    # write your code here

# example code to test your function
print(color((255,14,24))
print(color((245,255,233))
print(color((30, 200, 30))
print(color((240,240,22))

# example output
red
white
green
yellow