| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
""" Test the nearest matching hex color lookup """ |
| 5 |
|
| 6 |
import time |
| 7 |
from random import randint |
| 8 |
from colored.hexadecimal import Hex |
| 9 |
from colored import fore, back, style |
| 10 |
from colored.library import Library |
| 11 |
|
| 12 |
|
| 13 |
def compare_with_expected(in_hex, expected): |
| 14 |
""" Contribution by Fredrik Klasson. """ |
| 15 |
hexad = Hex() |
| 16 |
nearest = hexad.find(in_hex) |
| 17 |
|
| 18 |
|
| 19 |
match = Library.HEX_COLORS[nearest] == Library.HEX_COLORS[expected] |
| 20 |
|
| 21 |
e_str = '%s%s##%s' % (fore(expected), back(expected), style('reset')) |
| 22 |
n_str = '%s%s##%s' % (fore(nearest), back(nearest), style('reset')) |
| 23 |
print("%s: %s => %s = %s" % ('pass' if match else 'FAIL', in_hex, n_str, e_str)) |
| 24 |
|
| 25 |
return match |
| 26 |
|
| 27 |
|
| 28 |
def main(): |
| 29 |
""" Contribution by Fredrik Klasson. """ |
| 30 |
print(' Nearest Expected') |
| 31 |
test_set = { |
| 32 |
'1': ('#7f0000', '#800000', '#810000'), |
| 33 |
'2': ('#007f00', '#008000', '#008100'), |
| 34 |
'4': ('#00007f', '#000080', '#000081'), |
| 35 |
'10': ('#00fe00', '#00ff00', '#01ff00'), |
| 36 |
} |
| 37 |
|
| 38 |
all_ok = True |
| 39 |
for expected, hexes in test_set.items(): |
| 40 |
for hex_code in hexes: |
| 41 |
okay = compare_with_expected(hex_code, expected) |
| 42 |
all_ok = all_ok and okay |
| 43 |
|
| 44 |
try: |
| 45 |
T_sta = time.perf_counter() |
| 46 |
print('-' * 78) |
| 47 |
for y in range(0, 0xF): |
| 48 |
r_row = '' |
| 49 |
g_row = '' |
| 50 |
b_row = '' |
| 51 |
i_row = '' |
| 52 |
for x in range(0, 0xF): |
| 53 |
c = x + y * 0xF |
| 54 |
hex_code = '#%02x0000' % (c,) |
| 55 |
r_row += '%s%s#' % (fore(hex_code), back(hex_code)) |
| 56 |
hex_code = '#00%02x00' % (c,) |
| 57 |
g_row += '%s%s#' % (fore(hex_code), back(hex_code)) |
| 58 |
hex_code = '#0000%02x' % (c,) |
| 59 |
b_row += '%s%s#' % (fore(hex_code), back(hex_code)) |
| 60 |
hex_code = '#' + ('%02x' % (c,)) * 3 |
| 61 |
i_row += '%s%s#' % (fore(hex_code), back(hex_code)) |
| 62 |
print('%s%s %s%s %s%s %s%s' % ( |
| 63 |
r_row, style('reset'), g_row, style('reset'), b_row, style('reset'), i_row, style('reset'))) |
| 64 |
dT = time.perf_counter() - T_sta |
| 65 |
print('Lookup time: %0.4f s => %0.4f s/lookup' % (dT, dT / (2 * 4 * 0xFF))) |
| 66 |
print('-' * 78) |
| 67 |
except Exception as e: |
| 68 |
print('Whopsie, something %s-ish went wrong: %s' % (e.__class__.__name__, e)) |
| 69 |
import traceback |
| 70 |
traceback.print_exc() |
| 71 |
all_ok = False |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
try: |
| 76 |
T_sta = time.perf_counter() |
| 77 |
for y in range(0, 20): |
| 78 |
for x in range(0, 30): |
| 79 |
rnd = randint(0, 0xffffff) |
| 80 |
hex_code = '#%06x' % (rnd,) |
| 81 |
hexinv = '#%06x' % (0xffffff - rnd,) |
| 82 |
print('%s%s::' % (fore(hexinv), back(hex_code)), end='') |
| 83 |
print(style('reset')) |
| 84 |
dT = time.perf_counter() - T_sta |
| 85 |
print('Lookup time: %0.4f s => %0.4f s/lookup' % (dT, dT / (2 * 30 * 20))) |
| 86 |
except Exception as e: |
| 87 |
print('Whopsie, something %s-ish went wrong: %s' % (e.__class__.__name__, e)) |
| 88 |
all_ok = False |
| 89 |
|
| 90 |
return all_ok |
| 91 |
|
| 92 |
|
| 93 |
if __name__ == "__main__": |
| 94 |
ok = main() |
| 95 |
exit(0 if ok else 1) |