line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*- |
2
|
|
|
|
|
|
|
# vim:ts=8:sw=2:et:sta:sts=2 |
3
|
|
|
|
|
|
|
######### |
4
|
|
|
|
|
|
|
# Author: rmp |
5
|
|
|
|
|
|
|
# Last Modified: $Date: 2012-09-12 09:42:30 +0100 (Wed, 12 Sep 2012) $ |
6
|
|
|
|
|
|
|
# Id: $Id: Expr.pm 71 2012-09-12 08:42:30Z zerojinx $ |
7
|
|
|
|
|
|
|
# $HeadURL: https://text-sass.svn.sourceforge.net/svnroot/text-sass/trunk/lib/Text/Sass/Expr.pm $ |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package Text::Sass::Expr; |
10
|
29
|
|
|
29
|
|
1077
|
use strict; |
|
29
|
|
|
|
|
52
|
|
|
29
|
|
|
|
|
754
|
|
11
|
29
|
|
|
29
|
|
141
|
use warnings; |
|
29
|
|
|
|
|
52
|
|
|
29
|
|
|
|
|
818
|
|
12
|
29
|
|
|
29
|
|
146
|
use Carp; |
|
29
|
|
|
|
|
49
|
|
|
29
|
|
|
|
|
1658
|
|
13
|
29
|
|
|
29
|
|
24522
|
use Readonly; |
|
29
|
|
|
|
|
94876
|
|
|
29
|
|
|
|
|
51420
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = q[1.0.3]; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Readonly::Scalar our $SHADE_MAX => 255; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# yes, this should be tokenised and probably use overloading |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $OPS = { |
22
|
|
|
|
|
|
|
q[-] => sub { my ($x, $y) = @_; return $x - $y; }, |
23
|
|
|
|
|
|
|
q[+] => sub { my ($x, $y) = @_; return $x + $y; }, |
24
|
|
|
|
|
|
|
q[/] => sub { my ($x, $y) = @_; return $x / $y; }, |
25
|
|
|
|
|
|
|
q[*] => sub { my ($x, $y) = @_; return $x * $y; }, |
26
|
|
|
|
|
|
|
q[#-] => sub { my ($x, $y) = @_; |
27
|
|
|
|
|
|
|
my ($xr, $xg, $xb) = @{$x}; |
28
|
|
|
|
|
|
|
my ($yr, $yg, $yb) = @{$y}; |
29
|
|
|
|
|
|
|
my $nr = $xr-$yr; |
30
|
|
|
|
|
|
|
my $ng = $xg-$yg; |
31
|
|
|
|
|
|
|
my $nb = $xb-$yb; |
32
|
|
|
|
|
|
|
if($nr < 0) { $nr = 0; } |
33
|
|
|
|
|
|
|
if($ng < 0) { $ng = 0; } |
34
|
|
|
|
|
|
|
if($nb < 0) { $nb = 0; } |
35
|
|
|
|
|
|
|
return rgb_to_hex(undef,[$nr, $ng, $nb]); |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
q[#+] => sub { my ($x, $y) = @_; |
38
|
|
|
|
|
|
|
my ($xr, $xg, $xb) = @{$x}; |
39
|
|
|
|
|
|
|
my ($yr, $yg, $yb) = @{$y}; |
40
|
|
|
|
|
|
|
my $nr = $xr+$yr; |
41
|
|
|
|
|
|
|
my $ng = $xg+$yg; |
42
|
|
|
|
|
|
|
my $nb = $xb+$yb; |
43
|
|
|
|
|
|
|
if($nr > $SHADE_MAX) { $nr = $SHADE_MAX; } |
44
|
|
|
|
|
|
|
if($ng > $SHADE_MAX) { $ng = $SHADE_MAX; } |
45
|
|
|
|
|
|
|
if($nb > $SHADE_MAX) { $nb = $SHADE_MAX; } |
46
|
|
|
|
|
|
|
return rgb_to_hex(undef,[$nr, $ng, $nb]); |
47
|
|
|
|
|
|
|
}, |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Readonly::Scalar our $MM2CM => 0.1; |
51
|
|
|
|
|
|
|
Readonly::Scalar our $CM2MM => 10; |
52
|
|
|
|
|
|
|
Readonly::Scalar our $IN2CM => 2.54; |
53
|
|
|
|
|
|
|
Readonly::Scalar our $CM2IN => 1/2.54; |
54
|
|
|
|
|
|
|
Readonly::Scalar our $IN2MM => 25.4; |
55
|
|
|
|
|
|
|
Readonly::Scalar our $MM2IN => 1/25.4; |
56
|
|
|
|
|
|
|
Readonly::Scalar our $PC2PT => 12; |
57
|
|
|
|
|
|
|
Readonly::Scalar our $PT2PC => 1/12; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
our $CONV = { |
60
|
|
|
|
|
|
|
q[mm:cm] => sub { my ($v) = @_; return $v*$MM2CM; }, |
61
|
|
|
|
|
|
|
q[cm:mm] => sub { my ($v) = @_; return $v*$CM2MM; }, |
62
|
|
|
|
|
|
|
q[in:cm] => sub { my ($v) = @_; return $v*$IN2CM; }, |
63
|
|
|
|
|
|
|
q[cm:in] => sub { my ($v) = @_; return $v*$CM2IN; }, |
64
|
|
|
|
|
|
|
q[in:mm] => sub { my ($v) = @_; return $v*$IN2MM; }, |
65
|
|
|
|
|
|
|
q[mm:in] => sub { my ($v) = @_; return $v*$MM2IN; }, |
66
|
|
|
|
|
|
|
q[pc|pt] => sub { my ($v) = @_; return $v*$PC2PT; }, |
67
|
|
|
|
|
|
|
q[pt|pc] => sub { my ($v) = @_; return $v*$PT2PC; }, |
68
|
|
|
|
|
|
|
}; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub expr { |
71
|
15
|
|
|
15
|
1
|
1957
|
my ($pkg, $part1, $op, $part2) = @_; |
72
|
|
|
|
|
|
|
|
73
|
15
|
|
|
|
|
32
|
$part1 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi; |
74
|
15
|
|
|
|
|
34
|
$part2 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi; |
75
|
|
|
|
|
|
|
|
76
|
15
|
|
|
|
|
38
|
my ($p1, $u1) = @{$pkg->units($part1)}; |
|
15
|
|
|
|
|
47
|
|
77
|
15
|
|
|
|
|
37
|
my ($p2, $u2) = @{$pkg->units($part2)}; |
|
15
|
|
|
|
|
44
|
|
78
|
15
|
100
|
|
|
|
57
|
return if(!defined $p1); |
79
|
|
|
|
|
|
|
|
80
|
13
|
100
|
|
|
|
47
|
if(!$u1) { |
81
|
1
|
|
|
|
|
2
|
$u1 = q[]; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
13
|
100
|
|
|
|
32
|
if(!$u2) { |
85
|
8
|
|
|
|
|
16
|
$u2 = q[]; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
13
|
50
|
66
|
|
|
50
|
if(!$u1 && $u2) { |
89
|
0
|
|
|
|
|
0
|
$u1 = $u2; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
13
|
100
|
100
|
|
|
66
|
if(!$u2 && $u1) { |
93
|
7
|
|
|
|
|
12
|
$u2 = $u1; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
13
|
50
|
66
|
|
|
59
|
if($u1 ne $u2 && |
|
|
|
66
|
|
|
|
|
97
|
|
|
|
|
|
|
$u1 ne q[#] && |
98
|
|
|
|
|
|
|
$u2 ne q[#]) { |
99
|
2
|
|
|
|
|
6
|
$p2 = $pkg->convert($p2, $u2, $u1); |
100
|
2
|
|
|
|
|
4
|
$u2 = $u1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
13
|
100
|
|
|
|
45
|
if(!exists $OPS->{$op}) { |
104
|
1
|
50
|
|
|
|
4
|
if ($op =~ /^\w/smx) { |
|
|
0
|
|
|
|
|
|
105
|
1
|
|
|
|
|
3
|
return; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
elsif ($op =~ /\S{2,}/smx) { |
108
|
0
|
|
|
|
|
0
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
0
|
|
|
|
|
0
|
croak qq[Cannot "$op"]; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
12
|
100
|
|
|
|
55
|
if($u1 eq q[#]) { |
114
|
2
|
|
|
|
|
7
|
my $cb = $OPS->{"#$op"}; |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
50
|
|
|
16
|
return sprintf q[#%s], $cb->($p1||[0,0,0], $p2||[0,0,0]); |
|
|
|
50
|
|
|
|
|
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
10
|
|
50
|
|
|
72
|
return sprintf q[%s%s], $OPS->{$op}->($p1||0, $p2||0), $u1; |
|
|
|
50
|
|
|
|
|
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub units { |
123
|
88
|
|
|
88
|
1
|
2097
|
my ($pkg, $token) = @_; |
124
|
|
|
|
|
|
|
|
125
|
88
|
100
|
|
|
|
271
|
if($token =~ /^[#]/smx) { |
126
|
30
|
|
|
|
|
91
|
$token =~ s/^[#]//smx; |
127
|
30
|
|
|
|
|
85
|
return [$pkg->hex_to_rgb($token), q[#]]; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
58
|
|
|
|
|
248
|
my ($val, $units) = $token =~ /([\d.]+)(px|pt|pc|em|ex|mm|cm|in|%|)/smx; |
131
|
|
|
|
|
|
|
|
132
|
58
|
|
|
|
|
223
|
return [$val, $units]; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub rgb_to_hex { |
136
|
2
|
|
|
2
|
1
|
5
|
my ($pkg, $triple_ref) = @_; |
137
|
2
|
|
|
|
|
4
|
return sprintf q[%02x%02x%02x], @{$triple_ref}; |
|
2
|
|
|
|
|
26
|
|
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub hex_to_rgb { |
141
|
30
|
|
|
30
|
1
|
62
|
my ($pkg, $hex) = @_; |
142
|
|
|
|
|
|
|
|
143
|
30
|
|
|
|
|
117
|
my ($r, $g, $b) = unpack q[A2A2A2], $hex; |
144
|
30
|
|
|
|
|
169
|
return [hex $r, hex $g, hex $b]; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub convert { |
148
|
5
|
|
|
5
|
1
|
1904
|
my ($pkg, $val, $from, $to) = @_; |
149
|
|
|
|
|
|
|
|
150
|
5
|
|
|
|
|
11
|
my $fromto = "$from:$to"; |
151
|
5
|
50
|
|
|
|
16
|
if(!exists $CONV->{$fromto}) { |
152
|
0
|
|
|
|
|
0
|
croak qq[Cannot convert from $from to $to]; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
5
|
|
|
|
|
12
|
return $CONV->{$fromto}->($val); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |
159
|
|
|
|
|
|
|
__END__ |