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
|
30
|
|
|
30
|
|
1208
|
use strict; |
|
30
|
|
|
|
|
29
|
|
|
30
|
|
|
|
|
676
|
|
11
|
30
|
|
|
30
|
|
83
|
use warnings; |
|
30
|
|
|
|
|
31
|
|
|
30
|
|
|
|
|
541
|
|
12
|
30
|
|
|
30
|
|
77
|
use Carp; |
|
30
|
|
|
|
|
28
|
|
|
30
|
|
|
|
|
1208
|
|
13
|
30
|
|
|
30
|
|
12739
|
use Readonly; |
|
30
|
|
|
|
|
83881
|
|
|
30
|
|
|
|
|
34083
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = q[1.0.4]; |
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
|
1536
|
my ($pkg, $part1, $op, $part2) = @_; |
72
|
|
|
|
|
|
|
|
73
|
15
|
|
|
|
|
24
|
$part1 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi; |
74
|
15
|
|
|
|
|
22
|
$part2 =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi; |
75
|
|
|
|
|
|
|
|
76
|
15
|
|
|
|
|
14
|
my ($p1, $u1) = @{$pkg->units($part1)}; |
|
15
|
|
|
|
|
27
|
|
77
|
15
|
|
|
|
|
20
|
my ($p2, $u2) = @{$pkg->units($part2)}; |
|
15
|
|
|
|
|
21
|
|
78
|
15
|
100
|
|
|
|
36
|
return if(!defined $p1); |
79
|
|
|
|
|
|
|
|
80
|
13
|
100
|
|
|
|
31
|
if(!$u1) { |
81
|
1
|
|
|
|
|
1
|
$u1 = q[]; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
13
|
100
|
|
|
|
28
|
if(!$u2) { |
85
|
8
|
|
|
|
|
13
|
$u2 = q[]; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
13
|
50
|
66
|
|
|
29
|
if(!$u1 && $u2) { |
89
|
0
|
|
|
|
|
0
|
$u1 = $u2; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
13
|
100
|
100
|
|
|
59
|
if(!$u2 && $u1) { |
93
|
7
|
|
|
|
|
16
|
$u2 = $u1; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
13
|
50
|
66
|
|
|
43
|
if($u1 ne $u2 && |
|
|
|
66
|
|
|
|
|
97
|
|
|
|
|
|
|
$u1 ne q[#] && |
98
|
|
|
|
|
|
|
$u2 ne q[#]) { |
99
|
2
|
|
|
|
|
3
|
$p2 = $pkg->convert($p2, $u2, $u1); |
100
|
2
|
|
|
|
|
2
|
$u2 = $u1; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
13
|
100
|
|
|
|
28
|
if(!exists $OPS->{$op}) { |
104
|
1
|
50
|
|
|
|
3
|
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
|
|
|
|
37
|
if($u1 eq q[#]) { |
114
|
2
|
|
|
|
|
5
|
my $cb = $OPS->{"#$op"}; |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
50
|
|
|
11
|
return sprintf q[#%s], $cb->($p1||[0,0,0], $p2||[0,0,0]); |
|
|
|
50
|
|
|
|
|
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
10
|
|
50
|
|
|
50
|
return sprintf q[%s%s], $OPS->{$op}->($p1||0, $p2||0), $u1; |
|
|
|
50
|
|
|
|
|
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub units { |
123
|
88
|
|
|
88
|
1
|
1722
|
my ($pkg, $token) = @_; |
124
|
|
|
|
|
|
|
|
125
|
88
|
100
|
|
|
|
199
|
if($token =~ /^[#]/smx) { |
126
|
30
|
|
|
|
|
58
|
$token =~ s/^[#]//smx; |
127
|
30
|
|
|
|
|
63
|
return [$pkg->hex_to_rgb($token), q[#]]; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
58
|
|
|
|
|
196
|
my ($val, $units) = $token =~ /([\d.]+)(px|pt|pc|em|ex|mm|cm|in|%|)/smx; |
131
|
|
|
|
|
|
|
|
132
|
58
|
|
|
|
|
140
|
return [$val, $units]; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub rgb_to_hex { |
136
|
2
|
|
|
2
|
1
|
3
|
my ($pkg, $triple_ref) = @_; |
137
|
2
|
|
|
|
|
2
|
return sprintf q[%02x%02x%02x], @{$triple_ref}; |
|
2
|
|
|
|
|
15
|
|
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub hex_to_rgb { |
141
|
30
|
|
|
30
|
1
|
37
|
my ($pkg, $hex) = @_; |
142
|
|
|
|
|
|
|
|
143
|
30
|
|
|
|
|
93
|
my ($r, $g, $b) = unpack q[A2A2A2], $hex; |
144
|
30
|
|
|
|
|
115
|
return [hex $r, hex $g, hex $b]; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub convert { |
148
|
5
|
|
|
5
|
1
|
1252
|
my ($pkg, $val, $from, $to) = @_; |
149
|
|
|
|
|
|
|
|
150
|
5
|
|
|
|
|
6
|
my $fromto = "$from:$to"; |
151
|
5
|
50
|
|
|
|
11
|
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__ |