Output rgba(x,y,z,0) exactly as lessc

Currently a hack.
This commit is contained in:
Sascha Peilicke 2013-12-20 14:32:31 +01:00 committed by Sascha Peilicke
parent 7a041ce2a9
commit 74bee8c411
5 changed files with 27 additions and 4 deletions

View File

@ -84,13 +84,22 @@ class Color():
"""
if len(args) == 4:
try:
if float(list(args)[3]) > 1:
falpha = float(list(args)[3])
if falpha > 1:
args = args[:3]
if falpha == 0:
values = self._rgbatohex_raw(list(map(int, args)))
return "rgba(%s)" % ','.join([str(a) for a in values])
return self._rgbatohex(list(map(int, args)))
except ValueError:
if all((a for a in args
if a[-1] == '%'
and 100 >= int(a[:-1]) >= 0)):
alpha = list(args)[3]
if alpha[-1] == '%' and float(alpha[:-1]) == 0:
values = self._rgbatohex_raw([int(a[:-1]) * 255 / 100.0
for a in args])
return "rgba(%s)" % ','.join([str(a) for a in values])
return self._rgbatohex([int(a[:-1]) * 255 / 100.0
for a in args])
raise ValueError('Illegal color values')
@ -381,6 +390,14 @@ class Color():
return '#%s' % color
raise ValueError('Cannot format non-color')
def _rgbatohex_raw(self, rgba):
values = ["%x" % v for v in [0xff
if h > 0xff else
0 if h < 0 else h
for h in rgba]]
return values
def _rgbatohex(self, rgba):
return '#%s' % ''.join(["%02x" % v for v in
[0xff

View File

@ -163,6 +163,9 @@
color: #c8c9c8;
color: rgba(201,200,200,0.5);
color: rgba(202,200,200,0.0);
color: rgba(0,0,0,0.0);
color: rgba(1,0,0,0);
color: rgba(0,5,0,0);
}
.fraction {
color: #000000;

View File

@ -19,5 +19,5 @@
.mix{color:#7f007f;color:#7f7f7f;color:#7f9055;color:#3f00bf;color:#ff0000;color:#0000ff;}
.vars{color:#f6430f;background-color:#f8b38d;color:#f1dfda;}
.names{color:red;color:#0000cc;}
.alpha{color:#c8c8c8;color:#c9c8c8;color:#cac8c8;color:#c8c9c8;color:rgba(201,200,200,0.5);color:rgba(202,200,200,0.0);}
.alpha{color:#c8c8c8;color:#c9c8c8;color:#cac8c8;color:#c8c9c8;color:rgba(201,200,200,0.5);color:rgba(202,200,200,0.0);color:rgba(0,0,0,0.0);color:rgba(1,0,0,0);color:rgba(0,5,0,0);}
.fraction{color:#000000;color:#eeeeee;color:#fc0002;color:#292f33;color:#2e2e2e;color:#005c37;color:#555555;}

View File

@ -178,6 +178,9 @@
color: rgba(200, 201, 200, 100);
color: rgba(201, 200, 200, 0.5);
color: rgba(202, 200, 200, 0.0);
color: rgba(0, 0, 0, 0.0);
color: rgba(1, 0, 0, 0);
color: rgba(0%, 2%, 0%, 0%);
}
.fraction {
color: darken(#29332f, 45.3%);

View File

@ -43,11 +43,11 @@ class TestLessColor(unittest.TestCase):
for r, g, b, a, v in [
(255, 255, 255, 255, '#ffffff'),
(100, 100, 100, 100, '#646464'),
(0, 0, 0, 0, '#00000000'),
(0, 0, 0, 0, 'rgba(0,0,0,0)'),
('70%', '70%', '70%', '70%', '#b2b2b2b2'),
('1%', '1%', '1%', '1%', '#02020202'),
('100%', '100%', '100%', '100%', '#ffffffff'),
('0%', '0%', '0%', '0%', '#00000000'),
('0%', '0%', '0%', '0%', 'rgba(0,0,0,0)'),
]:
self.assertEqual(test(r, g, b, a), v)
for args in [