Mercurial > libervia-backend
comparison sat_frontends/tools/css_color.py @ 2624:56f94936df1e
code style reformatting using black
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 27 Jun 2018 20:14:46 +0200 |
parents | 26edcf3a30eb |
children | 003b8b4b56a7 |
comparison
equal
deleted
inserted
replaced
2623:49533de4540b | 2624:56f94936df1e |
---|---|
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.log import getLogger | 20 from sat.core.log import getLogger |
21 | |
21 log = getLogger(__name__) | 22 log = getLogger(__name__) |
22 | 23 |
23 | 24 |
24 CSS_COLORS = { | 25 CSS_COLORS = { |
25 u"black": u"000000", | 26 u"black": u"000000", |
165 u"turquoise": u"40e0d0", | 166 u"turquoise": u"40e0d0", |
166 u"violet": u"ee82ee", | 167 u"violet": u"ee82ee", |
167 u"wheat": u"f5deb3", | 168 u"wheat": u"f5deb3", |
168 u"whitesmoke": u"f5f5f5", | 169 u"whitesmoke": u"f5f5f5", |
169 u"yellowgreen": u"9acd32", | 170 u"yellowgreen": u"9acd32", |
170 u"rebeccapurple": u"663399" | 171 u"rebeccapurple": u"663399", |
171 } | 172 } |
172 DEFAULT = u"000000" | 173 DEFAULT = u"000000" |
173 | 174 |
174 | 175 |
175 def parse(raw_value, as_string=True): | 176 def parse(raw_value, as_string=True): |
183 else value is a 3 or 4 tuple of int (e.g.: (255, 0, 170, 187)). | 184 else value is a 3 or 4 tuple of int (e.g.: (255, 0, 170, 187)). |
184 If present, the 4th value is the alpha channel | 185 If present, the 4th value is the alpha channel |
185 If value can't be parsed, a warning message is logged, and DEFAULT is returned | 186 If value can't be parsed, a warning message is logged, and DEFAULT is returned |
186 """ | 187 """ |
187 raw_value = raw_value.strip().lower() | 188 raw_value = raw_value.strip().lower() |
188 if raw_value.startswith(u'#'): | 189 if raw_value.startswith(u"#"): |
189 # we have a hexadecimal value | 190 # we have a hexadecimal value |
190 str_value = raw_value[1:] | 191 str_value = raw_value[1:] |
191 if len(raw_value) in (3,4): | 192 if len(raw_value) in (3, 4): |
192 str_value = u''.join([2*v for v in str_value]) | 193 str_value = u"".join([2 * v for v in str_value]) |
193 elif raw_value.startswith(u'rgb'): | 194 elif raw_value.startswith(u"rgb"): |
194 left_p = raw_value.find(u'(') | 195 left_p = raw_value.find(u"(") |
195 right_p = raw_value.find(u')') | 196 right_p = raw_value.find(u")") |
196 rgb_values = [v.strip() for v in raw_value[left_p+1:right_p].split(',')] | 197 rgb_values = [v.strip() for v in raw_value[left_p + 1 : right_p].split(",")] |
197 expected_len = 4 if raw_value.startswith(u'rgba') else 3 | 198 expected_len = 4 if raw_value.startswith(u"rgba") else 3 |
198 if len(rgb_values) != expected_len: | 199 if len(rgb_values) != expected_len: |
199 log.warning(u"incorrect value: {}".format(raw_value)) | 200 log.warning(u"incorrect value: {}".format(raw_value)) |
200 str_value = DEFAULT | 201 str_value = DEFAULT |
201 else: | 202 else: |
202 int_values = [] | 203 int_values = [] |
203 for rgb_v in rgb_values: | 204 for rgb_v in rgb_values: |
204 p_idx = rgb_v.find(u'%') | 205 p_idx = rgb_v.find(u"%") |
205 if p_idx == -1: | 206 if p_idx == -1: |
206 # base 10 value | 207 # base 10 value |
207 try: | 208 try: |
208 int_v = int(rgb_v) | 209 int_v = int(rgb_v) |
209 if int_v > 255: | 210 if int_v > 255: |
220 raise ValueError(u"value exceed 255") | 221 raise ValueError(u"value exceed 255") |
221 int_values.append(int_v) | 222 int_values.append(int_v) |
222 except ValueError: | 223 except ValueError: |
223 log.warning(u"invalid percent value: {}".format(rgb_v)) | 224 log.warning(u"invalid percent value: {}".format(rgb_v)) |
224 int_values.append(0) | 225 int_values.append(0) |
225 str_value = u''.join([u"{:02x}".format(v) for v in int_values]) | 226 str_value = u"".join([u"{:02x}".format(v) for v in int_values]) |
226 elif raw_value.startswith(u'hsl'): | 227 elif raw_value.startswith(u"hsl"): |
227 log.warning(u"hue-saturation-lightness not handled yet") # TODO | 228 log.warning(u"hue-saturation-lightness not handled yet") # TODO |
228 str_value = DEFAULT | 229 str_value = DEFAULT |
229 else: | 230 else: |
230 try: | 231 try: |
231 str_value = CSS_COLORS[raw_value] | 232 str_value = CSS_COLORS[raw_value] |
234 str_value = DEFAULT | 235 str_value = DEFAULT |
235 | 236 |
236 if as_string: | 237 if as_string: |
237 return str_value | 238 return str_value |
238 else: | 239 else: |
239 return tuple([int(str_value[i]+str_value[i+1], 16) for i in xrange(0, len(str_value), 2)]) | 240 return tuple( |
241 [ | |
242 int(str_value[i] + str_value[i + 1], 16) | |
243 for i in xrange(0, len(str_value), 2) | |
244 ] | |
245 ) |