1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#!/usr/bin/env python3
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import re
import textwrap
from style_variable_generator.opacity import Opacity
from abc import ABC, abstractmethod
def split_args(arg_str):
'''Splits a string of args by comma, taking into account brackets.
'''
num_unmatched = 0
prev_index = 0
for i, c in enumerate(arg_str):
if c == '(':
num_unmatched += 1
elif c == ')':
num_unmatched -= 1
if (num_unmatched < 0):
raise ValueError('too many ")"')
elif c == ',' and num_unmatched == 0:
yield arg_str[prev_index:i].strip()
prev_index = i + 1
if num_unmatched > 0:
raise ValueError('too many "("')
yield arg_str[prev_index:].strip()
# Attempts to parse special variables, returns the Color if successful.
def from_white_black(var):
if var == 'white':
return ColorRGB([255, 255, 255])
if var == 'black':
return ColorRGB([0, 0, 0])
return None
def from_rgb_ref(rgb_ref):
match = re.match(r'^\$([a-z0-9_\.\-]+)\.rgb$', rgb_ref)
if not match:
raise ValueError(f'Expected a reference to an RGB variable: {rgb_ref}')
rgb_var = match.group(1)
color = from_white_black(rgb_var)
if color is None:
return ColorRGBVar(rgb_var + '.rgb')
return color
def ParseColor(value):
def ParseHex(value):
match = re.match(r'^#([0-9a-f]*)$', value)
if not match:
return None
value = match.group(1)
if len(value) != 6:
raise ValueError('Expected #RRGGBB')
return ColorRGB([int(x, 16) for x in textwrap.wrap(value, 2)],
Opacity(1))
def ParseRGB(value):
match = re.match(r'^rgb\((.*)\)$', value)
if not match:
return None
values = match.group(1).split(',')
if len(values) == 1:
color = from_rgb_ref(values[0])
color.opacity = Opacity(1)
return color
if len(values) == 3:
return ColorRGB([int(x) for x in values], Opacity(1))
raise ValueError('rgb() expected to have either 1 reference or 3 ints')
def ParseRGBA(value):
match = re.match(r'^rgba\((.*)\)$', value)
if not match:
return None
values = [x.strip() for x in match.group(1).split(',')]
if len(values) == 2:
color = from_rgb_ref(values[0])
color.opacity = Opacity(values[1])
return color
if len(values) == 4:
return ColorRGB([int(x) for x in values[0:3]], Opacity(values[3]))
raise ValueError('rgba() expected to have either'
'1 reference + alpha, or 3 ints + alpha')
def ParseBlend(value):
match = re.match(r'^blend\((.*)\)$', value)
if not match:
return None
values = list(split_args(match.group(1)))
if len(values) == 2:
# blend(color1, color2)
return ColorBlend([ParseColor(values[0]), ParseColor(values[1])])
elif len(values) == 3:
# blend(color1, blendPercentage%, color2)
blendPercentage = int(re.match(r'(\d+)%', values[1]).group(1))
return ColorBlend([ParseColor(values[0]),
ParseColor(values[2])], blendPercentage)
raise ValueError('Unexpected number of arguments for blend()')
def ParseVariableReference(value):
match = re.match(r'^\$([\w\.\-]+)$', value)
if not match:
return None
var = match.group(1)
color = from_white_black(var)
if color is not None:
color.opacity = Opacity(1)
return color
if value.endswith('.rgb'):
raise ValueError(
'color reference cannot resolve to an rgb reference')
return ColorVar(var)
parsers = [
ParseHex,
ParseRGB,
ParseRGBA,
ParseBlend,
ParseVariableReference,
]
value = re.sub(r'_rgb\b', '.rgb', value)
parsed = None
for p in parsers:
parsed = p(value)
if parsed is not None:
break
if parsed is None:
raise ValueError('Malformed color value')
if not isinstance(parsed, Color):
raise ValueError(repr(parsed))
return parsed
class Color:
'''A representation of a single color value.
This color can be of the following formats:
- #rrggbb
- rgb(r, g, b)
- rgba(r, g, b, a)
- rgba(r, g, b, $named_opacity)
- $other_color
- rgb($other_color.rgb)
- rgba($other_color.rgb, a)
- rgba($other_color.rgb, $named_opacity)
- blend(color1, color2)
NB: The color components that refer to other colors' RGB values must end
with '.rgb'.
'''
@abstractmethod
def GetFormula(self):
pass
@abstractmethod
def __repr__(self):
pass
class ColorRGB(Color):
def __init__(self, rgb=None, opacity=None):
super().__init__()
if rgb is None:
(self.r, self.g, self.b) = [-1, -1, -1]
else:
if not all([(0 <= v <= 255) for v in rgb]):
raise ValueError(f'RGB value out of bounds: {rgb}')
(self.r, self.g, self.b) = rgb
self.opacity = opacity
def GetFormula(self):
a = repr(self.opacity)
return 'rgba(%d, %d, %d, %s)' % (self.r, self.g, self.b, a)
def __repr__(self):
a = repr(self.opacity)
return 'rgba(%d, %d, %d, %s)' % (self.r, self.g, self.b, a)
class ColorRGBVar(Color):
def __init__(self, rgb_var=None, opacity=None):
super().__init__()
if not re.match(r'^[\w\.\-]+\.rgb$', rgb_var):
raise ValueError(f'"{rgb_var}" is not a valid RGBVar value')
self.rgb_var = rgb_var
self.opacity = opacity
def ToVar(self):
assert (self.rgb_var)
return self.rgb_var.replace('.rgb', '')
def GetFormula(self):
a = self.opacity.GetReadableStr()
return '%s @ %s' % (self.rgb_var, a)
def __repr__(self):
a = repr(self.opacity)
return 'rgba(var(--%s), %s)' % (self.rgb_var, a)
class ColorVar(Color):
def __init__(self, var=None):
super().__init__()
if not re.match(r'^[\w\.\-]+$', var):
raise ValueError(f'{var} is not a valid var value')
self.var = var
def GetFormula(self):
return self.var
def __repr__(self):
return 'var(--%s)' % self.var
class ColorBlend(Color):
'''This color is the result of blending two other colors
It uses the "A over B" operation, where A is blended_colors[0] and B is
blended_colors[1]. The mix percentace is `opacity`. If `opacity` is not
provided, the mix percentage may be taken from A's opacity.
'''
def __init__(self, colors=[], blendPercentage=None):
super().__init__()
if len(colors) not in [0, 2]:
raise ValueError(
f'Can only color-mix 2 colors. Found: {len(colors)}')
if not all(isinstance(c, Color) for c in colors):
raise ValueError(f'Non-Color found in {colors}')
self.blended_colors = colors
self.blendPercentage = blendPercentage
def GetFormula(self):
if self.blendPercentage is None:
return 'blend(%s, %s)' % (self.blended_colors[0].GetFormula(),
self.blended_colors[1].GetFormula())
return 'blend(%s, %s, %s)' % (self.blended_colors[0].GetFormula(),
self.blendPercentage,
self.blended_colors[1].GetFormula())
def __repr__(self):
return (
f'blend({repr(self.blended_colors)}, {repr(self.blendPercentage)})'
)
|