Skip to content

Commit df21073

Browse files
author
David Accomazzo
committed
add function to get all combos on a specified background of a specific contrast.
1 parent 376a82d commit df21073

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

colortest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* https://medium.muz.li/the-science-of-color-contrast-an-expert-designers-guide-33e84c41d156
1414
* Good article on accessibility contrast calculations
1515
* Might want to use this instead of color delta calculations
16+
* https://www.w3.org/TR/WCAG20/#relativeluminancedef
17+
* How WCAG defines contrast
1618
1719
TO DO:
1820
@@ -156,6 +158,16 @@ def getAllFgBgWithDiff(delta):
156158
color = f'\033[38;5;{foreground}m\033[48;5;{background}m {foreground} on {background}\033[0m. Contrast: {round(contrast)}'
157159
sys.stdout.write(color.ljust(4))
158160

161+
def printColorsByDeltaOnBg(background, delta):
162+
print(f'Printing all foreground colors that are delta {delta} on background {background}')
163+
for i in range(0, 256):
164+
contrast = getContrast(i, background)
165+
if contrast >= delta:
166+
foreground = str(i)
167+
_background = str(background)
168+
color = f'\033[38;5;{foreground}m\033[48;5;{_background}m {foreground} on {_background}\033[0m. Delta: {round(delta)}'
169+
sys.stdout.write(color.ljust(4))
170+
159171

160172
parser = argparse.ArgumentParser()
161173
subparsers = parser.add_subparsers(title='commands', dest='command')
@@ -179,6 +191,10 @@ def getAllFgBgWithDiff(delta):
179191
printContrastParser = subparsers.add_parser('filterContrast', help='Print out all the combos that meet a given delta.')
180192
printContrastParser.add_argument('DELTA', type=int, help='Pick a color delta')
181193

194+
printColorsByDeltaParser = subparsers.add_parser('printGoodContrasts', help='Print out all the combos on a certain bg of a specified delta.')
195+
printColorsByDeltaParser.add_argument('BACKGROUND', type = int, help='background, must be between 0 and 255')
196+
printColorsByDeltaParser.add_argument('DELTA', type = int, help='Delta, the specified difference level between colors')
197+
182198

183199
args = parser.parse_args()
184200

@@ -207,5 +223,7 @@ def getAllFgBgWithDiff(delta):
207223
testForegroundOnBackground(args.COLOR1, args.COLOR2)
208224
elif args.command == 'filterContrast':
209225
getAllFgBgWithDiff(args.DELTA)
226+
elif args.command == 'printGoodContrasts':
227+
printColorsByDeltaOnBg(args.BACKGROUND, args.DELTA)
210228
else:
211229
parser.print_help()

0 commit comments

Comments
 (0)