def test_nearest_on_square_distance(): kd = color_kd._build([ (Color(50, 50, 50), 255), (Color(50, 51, 50), 254), ]) assert color_kd.nearest(Color(0, 0, 0), kd) == 255 assert color_kd.nearest(Color(52, 52, 52), kd) == 254
def make_256() -> Optional[KD]: vals = (0, 95, 135, 175, 215, 255) colors = [(Color(r, g, b), i) for i, (r, g, b) in enumerate(itertools.product(vals, vals, vals), 16)] for i in range(24): v = 10 * i + 8 colors.append((Color(v, v, v), 232 + i)) return _build(colors)
def test_theme_default_settings_from_no_scope(): theme = Theme.from_dct({ 'tokenColors': [ { 'settings': { 'foreground': '#cccccc', 'background': '#333333' } }, ], }) assert theme.default.fg == Color.parse('#cccccc') assert theme.default.bg == Color.parse('#333333')
def from_dct(cls, dct: Dict[str, Any]) -> 'PartialStyle': kv = cls()._asdict() if 'foreground' in dct: kv['fg'] = Color.parse(dct['foreground']) if 'background' in dct: kv['bg'] = Color.parse(dct['background']) if dct.get('fontStyle') == 'bold': kv['b'] = True elif dct.get('fontStyle') == 'italic': kv['i'] = True elif dct.get('fontStyle') == 'underline': kv['u'] = True return cls(**kv)
def from_dct(cls, data: Dict[str, Any]) -> 'Theme': default = Style.blank()._asdict() for k in ('foreground', 'editor.foreground'): if k in data.get('colors', {}): default['fg'] = Color.parse(data['colors'][k]) break for k in ('background', 'editor.background'): if k in data.get('colors', {}): default['bg'] = Color.parse(data['colors'][k]) break root: Dict[str, Any] = {'children': {}} rules = data.get('tokenColors', []) + data.get('settings', []) for rule in rules: if 'scope' not in rule: scopes = [''] elif rule['scope'] == '': scopes = [''] elif isinstance(rule['scope'], str): scopes = [ s.strip() # some themes have a buggy trailing/leading comma for s in rule['scope'].strip().strip(',').split(',') if s.strip() ] else: scopes = rule['scope'] for scope in scopes: if ' ' in scope: # TODO: implement parent scopes continue elif scope == '': PartialStyle.from_dct(rule['settings']).overlay_on(default) continue cur = root for part in scope.split('.'): cur = cur['children'].setdefault(part, {'children': {}}) cur.update(rule['settings']) return cls(Style(**default), TrieNode.from_dct(root))
import pytest from babi.color import Color from babi.color_manager import _color_to_curses @pytest.mark.parametrize( ('color', 'expected'), ( (Color(0x00, 0x00, 0x00), (0, 0, 0)), (Color(0xff, 0xff, 0xff), (1000, 1000, 1000)), (Color(0x1e, 0x77, 0xd3), (117, 466, 827)), ), ) def test_color_to_curses(color, expected): assert _color_to_curses(color) == expected
def test_build_single_node(): kd = color_kd._build([(Color(0, 0, 0), 255)]) assert kd == color_kd._KD(Color(0, 0, 0), 255, left=None, right=None)
def test_smoke_kd_256(): kd_256 = color_kd.make_256() assert color_kd.nearest(Color(0, 0, 0), kd_256) == 16 assert color_kd.nearest(Color(0x1e, 0x77, 0xd3), kd_256) == 32
def test_nearest_one_node(): kd = color_kd._build([(Color(100, 100, 100), 99)]) assert color_kd.nearest(Color(0, 0, 0), kd) == 99
def test_nearest_trivial(): assert color_kd.nearest(Color(0, 0, 0), None) == 0
def test_build_many_colors(): kd = color_kd._build([ (Color(0, 106, 200), 255), (Color(1, 105, 201), 254), (Color(2, 104, 202), 253), (Color(3, 103, 203), 252), (Color(4, 102, 204), 251), (Color(5, 101, 205), 250), (Color(6, 100, 206), 249), ]) # each level is sorted by the next dimension assert kd == color_kd._KD( Color(3, 103, 203), 252, left=color_kd._KD( Color(1, 105, 201), 254, left=color_kd._KD(Color(2, 104, 202), 253, None, None), right=color_kd._KD(Color(0, 106, 200), 255, None, None), ), right=color_kd._KD( Color(5, 101, 205), 250, left=color_kd._KD(Color(6, 100, 206), 249, None, None), right=color_kd._KD(Color(4, 102, 204), 251, None, None), ), )
def test_color_parse(s, expected): assert Color.parse(s) == expected
import pytest from babi.color import Color @pytest.mark.parametrize( ('s', 'expected'), ( ('#1e77d3', Color(0x1e, 0x77, 0xd3)), ('white', Color(0xff, 0xff, 0xff)), ('black', Color(0x00, 0x00, 0x00)), ('#ccc', Color(0xcc, 0xcc, 0xcc)), ), ) def test_color_parse(s, expected): assert Color.parse(s) == expected