The following Python script compares a contrast of colored text (foreground) color against a background color. It is important to have good, high contrast. High contrast allows for easy readability and it’s easier on the eyes. This becomes more important when considering web page development to allow accessibility.
Sources
“How to calculate colour contrast” Accessibility Developer Guide (ADG*). An initiative of Access for all. Last Edited July 29, 2023. Accessed August 8, 2023. https://www.accessibility-developer-guide.com/knowledge/colours-and-contrast/how-to-calculate/
“Relative Luminance” Web Content Accessibility Guidelines (WCAG) 2.0. W3C, Inc. (World Wide Web Consortium, Inc.) Last Edited December 11, 2008. Accessed August 8, 2023. https://www.w3.org/TR/WCAG20/#relativeluminancedef
My math and calculator blog: edspi31415.blogspot.com
from math import * from kandinsky import * # 2023-08-13 EWS # Contrast - Numworks Version # Source: # www.w3.org, "WCAG 2.0" # ADG*, "How to calculate colour contrast" # CODE: # subs def lumin(r,g,b): r=compare(r/255) g=compare(g/255) b=compare(b/255) return .2126*r+.7152*g+.0722*b def compare(x): if x<=.03928: x=x/12.92 else: x=((x+.055)/1.055)**2.4 return x # main print("Color Contrast \n(0-255) \nBackground color:") r1=int(input("red? ")) g1=int(input("green? ")) b1=int(input("blue? ")) print("Text color:") r2=int(input("red? ")) g2=int(input("green? ")) b2=int(input("blue? ")) # calc l1=lumin(r1,g1,b1) l2=lumin(r2,g2,b2) cntr=(max(l1,l2)+.05)/(min(l1,l2)+.05) # draw c1=color(r1,g1,b1) c2=color(r2,g2,b2) cb=color(0,0,0) cw=color(255,255,255) fill_rect(0,0,320,240,c1) draw_string("Contrast test: Numworks",25,25,c2,c1) draw_string("Contrast: "+str(cntr),25,50,cb,cw) draw_string("Press OK to continue.",25,150,c2,c1)