color_tools.color_deficiency
Color vision deficiency simulation and correction.
This module provides functions to simulate how colors appear to individuals with various types of color blindness (color vision deficiency, CVD) and to apply corrections that can improve color discriminability for CVD individuals.
Supported deficiency types: - Protanopia: Red-blind (missing L-cones, ~1% of males) - Deuteranopia: Green-blind (missing M-cones, ~1% of males) - Tritanopia: Blue-blind (missing S-cones, ~0.001% of population)
- Example usage:
>>> # Simulate how red appears to someone with protanopia >>> simulated = simulate_protanopia((255, 0, 0)) >>> # Result will be darker, yellowish
>>> # Correct colors to improve discriminability for deuteranopia >>> corrected = correct_deuteranopia((0, 255, 0)) >>> # Result shifts green to be more distinguishable
- color_tools.color_deficiency.simulate_cvd(rgb, deficiency_type)[source]
Simulate how a color appears to someone with color vision deficiency.
This transforms colors to show how they would appear to individuals with various types of color blindness. Useful for testing color schemes for accessibility.
- Parameters:
deficiency_type (
str) – Type of CVD to simulate - ‘protanopia’ or ‘protan’: Red-blind - ‘deuteranopia’ or ‘deutan’: Green-blind - ‘tritanopia’ or ‘tritan’: Blue-blind - ‘all’: Combined average of all three types (universal accessibility diagnostic)
- Return type:
- Returns:
RGB color as it would appear to someone with the deficiency (0-255)
Example
>>> # See how red appears to someone with protanopia >>> simulate_cvd((255, 0, 0), 'protanopia') (145, 110, 0) # Appears darker, yellowish-brown
>>> # Test if two colors are distinguishable for deuteranopia >>> red_sim = simulate_cvd((255, 0, 0), 'deuteranopia') >>> green_sim = simulate_cvd((0, 255, 0), 'deuteranopia') >>> # If these are too similar, the colors may be confusing
>>> # Universal accessibility check — covers all three CVD types at once >>> simulate_cvd((255, 0, 0), 'all')
- color_tools.color_deficiency.simulate_protanopia(rgb)[source]
Simulate protanopia (red-blindness).
Protanopia is the absence of red cones (L-cones), affecting ~1% of males. Individuals with protanopia have difficulty distinguishing red from green, and red colors appear significantly darker.
- Parameters:
- Return type:
- Returns:
RGB color as it appears to someone with protanopia (0-255)
Example
>>> simulate_protanopia((255, 0, 0)) # Pure red (145, 110, 0) # Appears dark yellow-brown
- color_tools.color_deficiency.simulate_deuteranopia(rgb)[source]
Simulate deuteranopia (green-blindness).
Deuteranopia is the absence of green cones (M-cones), affecting ~1% of males. This is the most common form of color blindness. Individuals have difficulty distinguishing red from green, but unlike protanopia, reds don’t appear darker.
- Parameters:
- Return type:
- Returns:
RGB color as it appears to someone with deuteranopia (0-255)
Example
>>> simulate_deuteranopia((0, 255, 0)) # Pure green (159, 178, 0) # Appears yellowish
- color_tools.color_deficiency.simulate_tritanopia(rgb)[source]
Simulate tritanopia (blue-blindness).
Tritanopia is the absence of blue cones (S-cones), affecting ~0.001% of the population. This is very rare. Individuals have difficulty distinguishing blue from yellow/green.
- Parameters:
- Return type:
- Returns:
RGB color as it appears to someone with tritanopia (0-255)
Example
>>> simulate_tritanopia((0, 0, 255)) # Pure blue (0, 172, 133) # Appears cyan/turquoise
- color_tools.color_deficiency.simulate_all_cvd(rgb)[source]
Simulate all color vision deficiency types simultaneously.
Applies the combined (all-types) simulation matrix, which is the element-wise average of the protanopia, deuteranopia, and tritanopia simulation matrices. Each row sums to 1.0, so it is a valid color projection.
Useful as a universal accessibility diagnostic: if two colors appear similar after this transform, they are confusable to at least one of the three major CVD types.
- Parameters:
- Return type:
- Returns:
RGB color as viewed through the combined CVD simulation (0-255)
Example
>>> simulate_all_cvd((255, 0, 0)) # Pure red — universal worst-case view
- color_tools.color_deficiency.correct_cvd(rgb, deficiency_type)[source]
Apply color correction for color vision deficiency.
This transforms colors to improve discriminability for individuals with color blindness. The correction cannot restore missing color information, but can shift colors to utilize the remaining functional cone types more effectively.
Note: Corrected images should be viewed by individuals with the specific deficiency - they will not look “correct” to people with normal color vision.
- Parameters:
- Return type:
- Returns:
RGB color corrected for the deficiency (0-255)
Example
>>> # Correct red for protanopia viewers >>> correct_cvd((255, 0, 0), 'protanopia') (255, 0, 77) # Error shifted toward blue for contrast
>>> # Improve green discriminability for deuteranopia >>> correct_cvd((0, 255, 0), 'deuteranopia') (29, 255, 48) # Error shifted to add blue contrast
>>> # Universal correction — improves discriminability for all CVD types >>> correct_cvd((255, 0, 0), 'all')
- color_tools.color_deficiency.correct_protanopia(rgb)[source]
Correct colors for protanopia (red-blindness).
Shifts reds toward orange/yellow to increase visibility and improve discrimination between red and green for red-blind individuals.
- Parameters:
- Return type:
- Returns:
RGB color corrected for protanopia (0-255)
Example
>>> correct_protanopia((255, 0, 0)) # Pure red (255, 0, 77) # Error shifted toward blue for contrast
- color_tools.color_deficiency.correct_deuteranopia(rgb)[source]
Correct colors for deuteranopia (green-blindness).
Adjusts greens and reds to be more distinguishable for green-blind individuals by utilizing blue channel contrast.
- Parameters:
- Return type:
- Returns:
RGB color corrected for deuteranopia (0-255)
Example
>>> correct_deuteranopia((0, 255, 0)) # Pure green (29, 255, 48) # Error shifted to add blue contrast
- color_tools.color_deficiency.correct_tritanopia(rgb)[source]
Correct colors for tritanopia (blue-blindness).
Modifies blues and yellows to increase contrast for blue-blind individuals by enhancing red/green channel differences.
- Parameters:
- Return type:
- Returns:
RGB color corrected for tritanopia (0-255)
Example
>>> correct_tritanopia((0, 0, 255)) # Pure blue (85, 0, 255) # Error shifted toward red for contrast
- color_tools.color_deficiency.correct_all_cvd(rgb)[source]
Apply universal color correction for all CVD types simultaneously.
Applies the combined (all-types) correction matrix, which is the element-wise average of the protanopia, deuteranopia, and tritanopia correction matrices. The resulting matrix is perfectly symmetric: it redistributes the error signal equally across all three channels, improving discriminability for all CVD types at once.
Neutral colors (white, grey, black) are always preserved because the correction is applied to the error signal (original − simulated), not to the original pixel.
- Parameters:
- Return type:
- Returns:
RGB color corrected for all CVD types (0-255)
Example
>>> correct_all_cvd((255, 0, 0)) # Pure red — universal correction