From a4475951e75f8c4a14b74800057ca015e8840b99 Mon Sep 17 00:00:00 2001 From: Erik Osheim Date: Wed, 10 Jun 2009 23:41:53 -0400 Subject: [PATCH] closer color matching --HG-- branch : pmacs2 --- color.py | 36 ++++++++++++++++++++++++------------ mode/colortest.py | 26 +++++++++++++++----------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/color.py b/color.py index 286b753..4ee2776 100644 --- a/color.py +++ b/color.py @@ -28,6 +28,28 @@ def add_color(name, value, abbrev): ascii_map[abbrev] = name rev_ascii_map[name] = abbrev +def iter256(): + for r in range(0, 6): + for g in range(0, 6): + for b in range(0, 6): + if r >= g and g > b: name = 'yellow' + elif r >= b and b - g > 1: name = 'magenta' + + elif b >= g and g - r > 2: name = 'cyan' + elif b >= r and r > g: name = 'magenta' + + elif g >= r and r - b > 2: name = 'yellow' + elif g >= b and b - r > 1: name = 'cyan' + + elif r > b and r > g: name = 'red' + elif g > b and g > r: name = 'green' + elif b > r and b > g: name = 'blue' + + else: continue + + yield (name, r, g, b) + raise StopIteration + def color256(name, fallback, abbrev, r, g, b): name2 = '%s%d%d%d' % (name, r, g, b) abbrev2 = '%s%d%d%d' % (abbrev, r, g, b) @@ -84,18 +106,8 @@ def init(): value = curses.COLOR_WHITE add_color(name2, value, abbrev2) - for i in range(1, 6): - for j in range(0, i): - for k in range(0, i): - color256('red', curses.COLOR_RED, 'r', i, j, k) - color256('green', curses.COLOR_GREEN, 'g', j, i, k) - color256('blue', curses.COLOR_BLUE, 'b', j, k, i) - - for i in range(1, 6): - for j in range(0, i): - color256('yellow', curses.COLOR_YELLOW, 'y', i, i, j) - color256('cyan', curses.COLOR_CYAN, 'c', j, i, i) - color256('magenta', curses.COLOR_MAGENTA, 'm', i, j, i) + for name, r, g, b in iter256(): + color256(name, colors[name], rev_ascii_map[name], r, g, b) def build(fg, bg, *attr): v = curses.A_NORMAL | pairs(fg, bg) diff --git a/mode/colortest.py b/mode/colortest.py index a2c22f7..6027d65 100644 --- a/mode/colortest.py +++ b/mode/colortest.py @@ -1,3 +1,4 @@ +import color from mode import Fundamental from lex import Grammar, PatternRule @@ -17,17 +18,20 @@ def abc(name, r, g, b): ColortestGrammar.rules.append(PatternRule('z-' + name2, name2)) Colortest.colors['z-' + name2] = (name2, 'default') -for i in range(1, 6): - for j in range(0, i): - for k in range(0, i): - abc('red', i, j, k) - abc('green', j, i, k) - abc('blue', j, k, i) +for name, r, g, b in color.iter256(): + abc(name, r, g, b) -for i in range(1, 6): - for j in range(0, i): - abc('yellow', i, i, j) - abc('cyan', j, i, i) - abc('magenta', i, j, i) +#for i in range(1, 6): +# for j in range(0, i): +# for k in range(0, i): +# abc('red', i, j, k) +# abc('green', j, i, k) +# abc('blue', j, k, i) +# +#for i in range(1, 6): +# for j in range(0, i): +# abc('yellow', i, i, j) +# abc('cyan', j, i, i) +# abc('magenta', i, j, i) install = Colortest.install