|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# ----------------------------------------------------------------------------- |
|
3
|
|
|
# Copyright (c) 2015 Yann Lanthony |
|
4
|
|
|
# Copyright (c) 2017-2018 Spyder Project Contributors |
|
5
|
|
|
# |
|
6
|
|
|
# Licensed under the terms of the MIT License |
|
7
|
|
|
# (See LICENSE.txt for details) |
|
8
|
|
|
# ----------------------------------------------------------------------------- |
|
9
|
|
|
"""Libsass functions.""" |
|
10
|
|
|
|
|
11
|
|
|
# yapf: disable |
|
12
|
|
|
|
|
13
|
|
|
# Third party imports |
|
14
|
|
|
import sass |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
# yapf: enable |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def rgba(r, g, b, a): |
|
21
|
|
|
"""Convert r,g,b,a values to standard format. |
|
22
|
|
|
|
|
23
|
|
|
Where `a` is alpha! In CSS alpha can be given as: |
|
24
|
|
|
* float from 0.0 (fully transparent) to 1.0 (opaque) |
|
25
|
|
|
In Qt or qss that is: |
|
26
|
|
|
* int from 0 (fully transparent) to 255 (opaque) |
|
27
|
|
|
A percentage value 0% (fully transparent) to 100% (opaque) works |
|
28
|
|
|
in BOTH systems the same way! |
|
29
|
|
|
""" |
|
30
|
|
|
result = 'rgba({}, {}, {}, {}%)' |
|
31
|
|
|
if isinstance(r, sass.SassNumber): |
|
32
|
|
|
if a.unit == '%': |
|
33
|
|
|
alpha = a.value |
|
34
|
|
|
elif a.value > 1.0: |
|
35
|
|
|
# A value from 0 to 255 is coming in, convert to % |
|
36
|
|
|
alpha = a.value / 2.55 |
|
37
|
|
|
else: |
|
38
|
|
|
alpha = a.value * 100 |
|
39
|
|
|
return result.format( |
|
40
|
|
|
int(r.value), |
|
41
|
|
|
int(g.value), |
|
42
|
|
|
int(b.value), |
|
43
|
|
|
int(alpha), |
|
44
|
|
|
) |
|
45
|
|
|
elif isinstance(r, float): |
|
46
|
|
|
return result.format(int(r), int(g), int(b), int(a * 100)) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def rgba_from_color(color): |
|
50
|
|
|
""" |
|
51
|
|
|
Conform rgba. |
|
52
|
|
|
|
|
53
|
|
|
:type color: sass.SassColor |
|
54
|
|
|
""" |
|
55
|
|
|
# Inner rgba() call |
|
56
|
|
|
if not isinstance(color, sass.SassColor): |
|
57
|
|
|
return '{}'.format(color) |
|
58
|
|
|
|
|
59
|
|
|
return rgba(color.r, color.g, color.b, color.a) |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
def qlineargradient(x1, y1, x2, y2, stops): |
|
63
|
|
|
""" |
|
64
|
|
|
Implement qss qlineargradient function for scss. |
|
65
|
|
|
|
|
66
|
|
|
:type x1: sass.SassNumber |
|
67
|
|
|
:type y1: sass.SassNumber |
|
68
|
|
|
:type x2: sass.SassNumber |
|
69
|
|
|
:type y2: sass.SassNumber |
|
70
|
|
|
:type stops: sass.SassList |
|
71
|
|
|
:return: |
|
72
|
|
|
""" |
|
73
|
|
|
stops_str = [] |
|
74
|
|
|
for stop in stops[0]: |
|
75
|
|
|
pos, color = stop[0] |
|
76
|
|
|
stops_str.append('stop: {} {}'.format( |
|
77
|
|
|
pos.value, |
|
78
|
|
|
rgba_from_color(color), |
|
79
|
|
|
)) |
|
80
|
|
|
template = 'qlineargradient(x1: {}, y1: {}, x2: {}, y2: {}, {})' |
|
81
|
|
|
return template.format(x1.value, y1.value, x2.value, y2.value, |
|
82
|
|
|
', '.join(stops_str)) |
|
83
|
|
|
|