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
|
25
|
|
|
25
|
|
1066
|
use strict; |
|
25
|
|
|
|
|
42
|
|
|
25
|
|
|
|
|
689
|
|
11
|
25
|
|
|
25
|
|
120
|
use warnings; |
|
25
|
|
|
|
|
39
|
|
|
25
|
|
|
|
|
628
|
|
12
|
25
|
|
|
25
|
|
114
|
use Carp; |
|
25
|
|
|
|
|
43
|
|
|
25
|
|
|
|
|
1368
|
|
13
|
25
|
|
|
25
|
|
19651
|
use Readonly; |
|
25
|
|
|
|
|
78839
|
|
|
25
|
|
|
|
|
43676
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = q[1.0.0]; |
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
|
14
|
|
|
14
|
1
|
1534
|
my ($pkg, $part1, $op, $part2) = @_; |
72
|
|
|
|
|
|
|
|
73
|
14
|
|
|
|
|
27
|
$part1 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi; |
74
|
14
|
|
|
|
|
33
|
$part2 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi; |
75
|
|
|
|
|
|
|
|
76
|
14
|
|
|
|
|
21
|
my ($p1, $u1) = @{$pkg->units($part1)}; |
|
14
|
|
|
|
|
43
|
|
77
|
14
|
|
|
|
|
30
|
my ($p2, $u2) = @{$pkg->units($part2)}; |
|
14
|
|
|
|
|
39
|
|
78
|
14
|
100
|
|
|
|
53
|
return if(!defined $p1); |
79
|
|
|
|
|
|
|
|
80
|
12
|
100
|
|
|
|
38
|
if(!$u1) { |
81
|
1
|
|
|
|
|
3
|
$u1 = q[]; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
12
|
100
|
|
|
|
28
|
if(!$u2) { |
85
|
7
|
|
|
|
|
27
|
$u2 = q[]; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
12
|
50
|
66
|
|
|
48
|
if(!$u1 && $u2) { |
89
|
0
|
|
|
|
|
0
|
$u1 = $u2; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
12
|
100
|
100
|
|
|
60
|
if(!$u2 && $u1) { |
93
|
6
|
|
|
|
|
12
|
$u2 = $u1; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
12
|
50
|
66
|
|
|
50
|
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
|
12
|
100
|
|
|
|
41
|
if(!exists $OPS->{$op}) { |
104
|
1
|
50
|
|
|
|
4
|
if ($op =~ /^\w/smx) { |
|
|
0
|
|
|
|
|
|
105
|
1
|
|
|
|
|
4
|
return; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
elsif ($op =~ /\S{2,}/smx) { |
108
|
0
|
|
|
|
|
0
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
0
|
|
|
|
|
0
|
croak qq[Cannot "$op"]; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
11
|
100
|
|
|
|
53
|
if($u1 eq q[#]) { |
114
|
2
|
|
|
|
|
7
|
my $cb = $OPS->{"#$op"}; |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
50
|
|
|
14
|
return sprintf q[#%s], $cb->($p1||[0,0,0], $p2||[0,0,0]); |
|
|
|
50
|
|
|
|
|
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
9
|
|
50
|
|
|
49
|
return sprintf q[%s%s], $OPS->{$op}->($p1||0, $p2||0), $u1; |
|
|
|
50
|
|
|
|
|
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub units { |
123
|
79
|
|
|
79
|
1
|
1569
|
my ($pkg, $token) = @_; |
124
|
|
|
|
|
|
|
|
125
|
79
|
100
|
|
|
|
238
|
if($token =~ /^[#]/smx) { |
126
|
27
|
|
|
|
|
90
|
$token =~ s/^[#]//smx; |
127
|
27
|
|
|
|
|
79
|
return [$pkg->hex_to_rgb($token), q[#]]; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
52
|
|
|
|
|
209
|
my ($val, $units) = $token =~ /([\d.]+)(px|pt|pc|em|ex|mm|cm|in|%|)/smx; |
131
|
|
|
|
|
|
|
|
132
|
52
|
|
|
|
|
195
|
return [$val, $units]; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub rgb_to_hex { |
136
|
2
|
|
|
2
|
1
|
4
|
my ($pkg, $triple_ref) = @_; |
137
|
2
|
|
|
|
|
4
|
return sprintf q[%02x%02x%02x], @{$triple_ref}; |
|
2
|
|
|
|
|
19
|
|
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub hex_to_rgb { |
141
|
27
|
|
|
27
|
1
|
46
|
my ($pkg, $hex) = @_; |
142
|
|
|
|
|
|
|
|
143
|
27
|
|
|
|
|
102
|
my ($r, $g, $b) = unpack q[A2A2A2], $hex; |
144
|
27
|
|
|
|
|
148
|
return [hex $r, hex $g, hex $b]; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub convert { |
148
|
5
|
|
|
5
|
1
|
1451
|
my ($pkg, $val, $from, $to) = @_; |
149
|
|
|
|
|
|
|
|
150
|
5
|
|
|
|
|
11
|
my $fromto = "$from:$to"; |
151
|
5
|
50
|
|
|
|
13
|
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__ |