4 Function Design Recipe: Colour Vision Deficiency Filters (part 2)
Alisha Hasan and Rutwa Engineer
Case Study – Colour Vision Deficiency Filters
Colour vision deficiency, commonly known as colour blindness, affects millions of people worldwide and can make distinguishing certain colours challenging. This case study will explore how we can use programming and the FDR to address this issue, improving accessibility for those with colour vision deficiencies.
Understanding Colour Vision Deficiency
Colour vision deficiency can impact how individuals interact with digital content, making it crucial for developers to consider accessible design. We can create functions that adapt colour schemes to be more inclusive.
There are several types of colour vision deficiencies, each affecting colour perception differently:
- Protanopia: Difficulty differentiating red and green colours due to lack of or dysfunctional red cone cells in the eyes.
- Deuteranopia: Difficulty differentiating red and green colours due to lack of or dysfunctional green cone cells in the eyes.
- Tritanopia: Difficulty differentiating blue and yellow colours due to the lack of or dysfunctional blue cone cells in the eyes.
The Function Design Recipe
Through this case study, we will follow the function design recipe to:
- Identify colours that are hard to distinguish for individuals with different forms of colour vision deficiency.
- Modify these colours to more accessible alternatives.
- Simulate different types of colour vision deficiencies to test our solutions.
Lists and Tuples
Before diving into your task, it’s essential to understand two fundamental data structures in Python: lists and tuples. These data structures will help us organize and manipulate collections of values, such as RGB colour codes.
Lists
A list is a collection of items that are ordered and mutable, which means they can be changed. Lists are created using square brackets ‘[]’, and each item in the list is separated by a comma. Lists can store multiple values of different data types, including other lists. Here are some examples:
# A list of integers
numbers = [1, 2, 3, 4, 5]
# A list of strings
colours = ['red', 'green', 'blue']
# A list containing different data types
mixed = [1, 'hello', 3.14, True]
We can also access elements in a list. Each element’s “position” can be referred to as an “index.” In Python, we use something called “zero-indexing”, which means the first element is stored in index 0, the second element is stored in index 1, and so on. To access an element at a certain index, all we need to do is take the list name, followed by some square brackets with the index number in between them, like so:
# Accessing the first element in a list
first_colour = colours[0] # 'red'
Lists are mutable, meaning you can change, add, or remove elements after the list has been created:
# Modifying an element in the list
colours[1] = 'yellow' # colours becomes ['red', 'yellow', 'blue']
# Adding a new element to the end of the list
colours.append('purple') # colours becomes ['red', 'yellow', 'blue', 'purple']
# Removing an element from the list
colours.remove('red') # colours becomes ['yellow', 'blue', 'purple']
Tuples
A tuple is similar to a list but has a key difference: tuples are immutable, meaning that once a tuple is created, its elements cannot be changed. Tuples are created using parentheses ‘()’. For example:
# A tuple of integers
coordinates = (10, 20)
# A tuple of strings
fruits = ('apple', 'banana', 'cherry')
# A tuple containing different data types
info = (42, 'answer', 3.14)
# Accessing elements in a tuple
first_fruit = fruits[0] # 'apple'
Because tuples are immutable, they are often used to store data that should not be modified. This immutability can help ensure the integrity of the data throughout the program. In this case study, we will use tuples to represent RGB colours, ensuring that the colour values remain consistent throughout our calculations. By understanding lists and tuples, you’ll be better prepared to manipulate and process collections of data in Python.
Consider the following Python function that adjusts colours for different types of colour vision deficiencies. In the implementation, we use numeric values to represent colours in the RGB (Red, Green, Blue) colour model. This model consists of a three-element integer tuple that represents a colour, where the first, second, and third elements respectively determine how much red, green, or blue is present in the colour on a scale from 0 to 255. Using this model allows us to perform precise adjustments based on the type of colour vision deficiency. By altering the intensity of specific colour channels, we can make colours more distinguishable for individuals with different types of colour vision deficiencies.
def adjust_colour_for_cvd(color: Tuple[int, int, int], deficiency_type: str) -> Tuple[int, int, int]: """ Adjust the given colour to be more distinguishable for those with colour vision deficiency. Parameters: colour: A tuple representing an RGB colour (R, G, B). deficiency_type: A string representing the type of colour vision deficiency ('protanopia', 'deuteranopia', 'tritanopia'). >>> adjust_colour_for_cvd((255, 0, 0), 'protanopia') (112, 0, 0) >>> adjust_colour_for_cvd((0, 255, 0), 'deuteranopia') (0, 112, 0) >>> adjust_colour_for_cvd((0, 0, 255), 'tritanopia') (0, 0, 112) """
r = color[0] g = color[1] b = color[2]
if deficiency_type == 'protanopia': r = int(r * 0.44) elif deficiency_type == 'deuteranopia': g = int(g * 0.44) elif deficiency_type == 'tritanopia': b = int(b * 0.44) # return new tuple with adjusted colour return (r, g, b)
- The adjustment factor of 0.44 is used in the function. Why do you think this specific value was chosen, and how might you determine a more accurate factor?
- Consider the tuple (255, 255, 0) for the colour yellow. How would this colour be adjusted for each type of colour vision deficiency using the current function logic? Can you name one limitation of this approach?
- Why is it important to test the function with a variety of colours and deficiency types? Describe a testing strategy that could be used to ensure the function works correctly.