Merge branch 'better-gradients' into develop

This commit is contained in:
ZyX 2014-07-06 11:57:49 +04:00
commit 184df196b9
3 changed files with 76 additions and 40 deletions

View File

@ -75,8 +75,8 @@
}, },
"gradients": { "gradients": {
"dark_GREEN_Orange_red": [ "dark_GREEN_Orange_red": [
[22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 94, 94, 94, 94, 94, 94, 94, 52], [22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 94, 94, 94, 94, 94, 94, 94, 88, 52],
["005f00", "015f00", "025f00", "035f00", "045f00", "055f00", "065f00", "075f00", "085f00", "095f00", "0b5f00", "0c5f00", "0d5f00", "0e5f00", "0f5f00", "105f00", "115f00", "125f00", "135f00", "145f00", "165f00", "175f00", "185f00", "195f00", "1a5f00", "1b5f00", "1c5f00", "1d5f00", "1e5f00", "1f5f00", "215f00", "225f00", "235f00", "245f00", "255f00", "265f00", "275f00", "285f00", "295f00", "2a5f00", "2c5f00", "2d5f00", "2e5f00", "2f5f00", "305f00", "315f00", "325f00", "335f00", "345f00", "355f00", "375f00", "385f00", "395f00", "3a5f00", "3b5f00", "3c5f00", "3d5f00", "3e5f00", "3f5f00", "415f00", "425f00", "435f00", "445f00", "455f00", "465f00", "475f00", "485f00", "495f00", "4a5f00", "4c5f00", "4d5f00", "4e5f00", "4f5f00", "505f00", "515f00", "525f00", "535f00", "545f00", "555f00", "575f00", "585f00", "595f00", "5a5f00", "5b5f00", "5c5f00", "5d5f00", "5e5f00", "615f00", "655f00", "685f00", "6c5f00", "6f5f00", "735f00", "765f00", "7a5f00", "7d5f00", "815f00", "845f00", "815200", "702900"] ["006000", "006000", "006000", "006000", "006000", "006000", "006000", "006000", "006000", "036000", "076000", "0a6000", "0d6000", "106000", "126000", "146000", "166000", "186000", "1a6000", "1b6000", "1d6000", "1e6000", "206000", "216000", "236000", "246000", "256000", "266000", "286000", "296000", "2a6000", "2b6000", "2c6100", "2d6100", "2f6100", "306100", "316100", "326100", "336100", "346100", "356100", "366100", "376100", "386100", "386100", "396100", "3a6100", "3b6100", "3c6100", "3d6100", "3e6100", "3f6100", "406100", "406100", "416100", "426000", "436000", "446000", "456000", "456000", "466000", "476000", "486000", "496000", "496000", "4a6000", "4b6000", "4c6000", "4d6000", "4d6000", "4e6000", "4f6000", "506000", "506000", "516000", "526000", "536000", "536000", "546000", "556000", "566000", "566000", "576000", "586000", "596000", "596000", "5a6000", "5d6000", "616000", "646000", "686000", "6b6000", "6f6000", "726000", "766000", "796000", "7d6000", "806000", "7e5500", "6f3105", "5d0001"]
], ],
"GREEN_Orange_red": [ "GREEN_Orange_red": [
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1],

View File

@ -1,39 +1,58 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import division, print_function
import sys import sys
import os import os
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
def get_color(name, rgb): def get_lab(name, rgb):
return name, (int(rgb[:2], 16), int(rgb[2:4], 16), int(rgb[4:6], 16)) rgb = sRGBColor(int(rgb[:2], 16), int(rgb[2:4], 16), int(rgb[4:6], 16),
is_upscaled=True)
lab = convert_color(rgb, LabColor)
return name, lab
with open(os.path.join(os.path.dirname(__file__), 'colors.map'), 'r') as f: with open(os.path.join(os.path.dirname(__file__), 'colors.map'), 'r') as f:
colors = [get_color(*line.split('\t')) for line in f] colors = [get_lab(*line.split('\t')) for line in f]
urgb = get_color(None, sys.argv[1])[1] ulab = get_lab(None, sys.argv[1])[1]
def col_distance(rgb1, rgb2):
return sum(((rgb1[i] - rgb2[i]) ** 2 for i in range(3)))
def find_color(urgb, colors): def find_color(urgb, colors):
cur_distance = 3 * (255 ** 2 + 1) cur_distance = 3 * (255 ** 2 + 1)
cur_color = None cur_color = None
for color, crgb in colors: for color, clab in colors:
dist = col_distance(urgb, crgb) dist = delta_e_cie2000(ulab, clab)
if dist < cur_distance: if dist < cur_distance:
cur_distance = dist cur_distance = dist
cur_color = (color, crgb) cur_color = (color, clab)
return cur_color return cur_color
cur_color = find_color(urgb, colors) cur_color = find_color(ulab, colors)
print urgb, ':', cur_color
col_1 = ';2;' + ';'.join((str(i) for i in urgb)) + 'm' def lab_to_csi(lab):
col_2 = ';2;' + ';'.join((str(i) for i in cur_color[1])) + 'm' rgb = convert_color(lab, sRGBColor)
colstr = ';2;' + ';'.join((str(i) for i in get_upscaled_values(rgb)))
return colstr + 'm'
def get_upscaled_values(rgb):
return [min(max(0, i), 255) for i in rgb.get_upscaled_value_tuple()]
def get_rgb(lab):
rgb = convert_color(lab, sRGBColor)
rgb = sRGBColor(*get_upscaled_values(rgb), is_upscaled=True)
return rgb.get_rgb_hex()[1:]
print(get_rgb(ulab), ':', cur_color[0], ':', get_rgb(cur_color[1]))
col_1 = lab_to_csi(ulab)
col_2 = lab_to_csi(cur_color[1])
sys.stdout.write('\033[48' + col_1 + '\033[38' + col_2 + 'abc\033[0m <-- bg:urgb, fg:crgb\n') sys.stdout.write('\033[48' + col_1 + '\033[38' + col_2 + 'abc\033[0m <-- bg:urgb, fg:crgb\n')
sys.stdout.write('\033[48' + col_2 + '\033[38' + col_1 + 'abc\033[0m <-- bg:crgb, fg:urgb\n') sys.stdout.write('\033[48' + col_2 + '\033[38' + col_1 + 'abc\033[0m <-- bg:crgb, fg:urgb\n')

View File

@ -8,6 +8,9 @@ import json
from powerline.colorscheme import cterm_to_hex from powerline.colorscheme import cterm_to_hex
from itertools import groupby from itertools import groupby
import argparse import argparse
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
try: try:
from __builtin__ import unicode from __builtin__ import unicode
@ -22,15 +25,20 @@ def num2(s):
return (False, [float(v) for v in s.partition(' ')[::2]]) return (False, [float(v) for v in s.partition(' ')[::2]])
def rgbint_to_rgb(rgbint): def rgbint_to_lab(rgbint):
return ((rgbint >> 16) & 0xFF, (rgbint >> 8) & 0xFF, rgbint & 0xFF) rgb = sRGBColor((rgbint >> 16) & 0xFF, (rgbint >> 8) & 0xFF, rgbint & 0xFF,
is_upscaled=True)
return convert_color(rgb, LabColor)
cterm_to_lab = tuple((rgbint_to_lab(v) for v in cterm_to_hex))
def color(s): def color(s):
if len(s) <= 3: if len(s) <= 3:
return rgbint_to_rgb(cterm_to_hex[int(s)]) return cterm_to_lab[int(s)]
else: else:
return rgbint_to_rgb(int(s, 16)) return rgbint_to_lab(int(s, 16))
def nums(s): def nums(s):
@ -53,34 +61,42 @@ def linear_gradient(start_value, stop_value, start_offset, stop_offset, offset):
return start_value + ((offset - start_offset) * (stop_value - start_value) / (stop_offset - start_offset)) return start_value + ((offset - start_offset) * (stop_value - start_value) / (stop_offset - start_offset))
def gradient(DATA): def lab_gradient(slab, elab, soff, eoff, off):
svals = slab.get_value_tuple()
evals = elab.get_value_tuple()
return LabColor(*[linear_gradient(start_value, end_value, soff, eoff, off)
for start_value, end_value in zip(svals, evals)])
def generate_gradient_function(DATA):
def gradient_function(y): def gradient_function(y):
initial_offset = 0 initial_offset = 0
for offset, start, end in DATA: for offset, start, end in DATA:
if y <= offset: if y <= offset:
return [linear_gradient(start[i], end[i], initial_offset, offset, y) for i in range(3)] return lab_gradient(start, end, initial_offset, offset, y)
initial_offset = offset initial_offset = offset
return gradient_function return gradient_function
def get_rgb(*args): def get_upscaled_values(rgb):
return "%02x%02x%02x" % args return [min(max(0, i), 255) for i in rgb.get_upscaled_value_tuple()]
def col_distance(rgb1, rgb2): def get_rgb(lab):
return sum(((rgb1[i] - rgb2[i]) ** 2 for i in range(3))) rgb = convert_color(lab, sRGBColor)
rgb = sRGBColor(*get_upscaled_values(rgb), is_upscaled=True)
return rgb.get_rgb_hex()[1:]
def find_color(urgb, colors, ctrans): def find_color(ulab, colors, ctrans):
cur_distance = 3 * (255 ** 2 + 1) cur_distance = float('inf')
cur_color = None cur_color = None
i = 0 i = 0
for crgbint in colors: for clab in colors:
crgb = rgbint_to_rgb(crgbint) dist = delta_e_cie2000(ulab, clab)
dist = col_distance(urgb, crgb)
if dist < cur_distance: if dist < cur_distance:
cur_distance = dist cur_distance = dist
cur_color = (ctrans(i), crgb) cur_color = (ctrans(i), clab)
i += 1 i += 1
return cur_color return cur_color
@ -89,7 +105,8 @@ def print_color(color):
if type(color) is int: if type(color) is int:
colstr = '5;' + str(color) colstr = '5;' + str(color)
else: else:
colstr = '2;' + ';'.join((str(int(round(i))) for i in color)) rgb = convert_color(color, sRGBColor)
colstr = '2;' + ';'.join((str(i) for i in get_upscaled_values(rgb)))
sys.stdout.write('\033[48;' + colstr + 'm ') sys.stdout.write('\033[48;' + colstr + 'm ')
@ -131,15 +148,15 @@ else:
steps = [i * step for i in range(1, maxweight + 1)] steps = [i * step for i in range(1, maxweight + 1)]
data = [(weight, args.gradient[i - 1], args.gradient[i]) for weight, i in zip(steps, range(1, len(args.gradient)))] data = [(weight, args.gradient[i - 1], args.gradient[i]) for weight, i in zip(steps, range(1, len(args.gradient)))]
gr_func = gradient(data) gr_func = generate_gradient_function(data)
gradient = [gr_func(y) for y in range(0, m)] gradient = [gr_func(y) for y in range(0, m)]
palettes = { palettes = {
'16': (cterm_to_hex[:16], lambda c: c), '16': (cterm_to_lab[:16], lambda c: c),
'256': (cterm_to_hex, lambda c: c), '256': (cterm_to_lab, lambda c: c),
None: (cterm_to_hex[16:], lambda c: c + 16), None: (cterm_to_lab[16:], lambda c: c + 16),
} }
r = [get_rgb(*col) for col in gradient] r = [get_rgb(lab) for lab in gradient]
r2 = [find_color(col, *palettes[args.palette])[0] for col in gradient] r2 = [find_color(lab, *palettes[args.palette])[0] for lab in gradient]
r3 = [i[0] for i in groupby(r2)] r3 = [i[0] for i in groupby(r2)]
print(json.dumps(r)) print(json.dumps(r))
print(json.dumps(r2)) print(json.dumps(r2))