blib/lib/Text/Sass/Functions.pm

filter: Statementstmt 96.9% 129 / 133 Branchbran 64.2% 9 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 34 / 34 Podpod 100% 25 / 25 totaltotal 95.6% 200 / 209 CC 9 SCAR 4.5
CC
9
Cov
94%
_value
4.1
quote
3.1
_color
2.1
CRAP 1.6
« blib/script/sass2css.pl ↑ Summary blib/lib/Text/Sass/Token.pm »
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: bolav
5 # Last Modified: $Date: 2012-09-12 09:42:30 +0100 (Wed, 12 Sep 2012) $
6 # Id: $Id: Functions.pm 71 2012-09-12 08:42:30Z zerojinx $
7 # $HeadURL: https://text-sass.svn.sourceforge.net/svnroot/text-sass/trunk/lib/Text/Sass/Functions.pm $
8 #
9 package Text::Sass::Functions;
10 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use Convert::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 30 ▶use Text::Sass::Expr;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 30 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 30 our $VERSION = q[1.0.4];
19 30 Readonly::Scalar my $PERC => 100;
20  
21 sub _color {
22 25 ▶ my ($self, $color) = @_;
Subroutine _color: called CC=2 SCAR=7.2
23  
24 25 $color =~ s/[#](.)(.)(.)(\b)/#${1}${1}${2}${2}${3}${3}$4/smxgi;
25 25 $color = Text::Sass::Expr->units($color);
26  
27 25 ▶ if ($color->[1] eq q[#]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color->[1] eq "#") 25 0 25
28 25 return Convert::Color->new(q[rgb8:].$color->[0]->[0].q[,].$color->[0]->[1].q[,].$color->[0]->[2]);
29 }
30 0 croak 'not a color '.$color;
31 }
32  
33 sub _value {
34 16 ▶ my ($self, $value) = @_;
Subroutine _value: called CC=3 SCAR=14.2
35  
36 16 $value = Text::Sass::Expr->units($value);
37  
38 16 ▶ if ($value->[1] eq q[%]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value->[1] eq '%') { } 16 0 16
elsif ($value->[1] eq '') { } 0 0 0
39 16 return $value->[0] / $PERC;
40  
41 } elsif ($value->[1] eq q[]) {
42 0 return $value->[0];
43 }
44  
45 0 croak 'Unknown unit '.$value->[1].' for value';
46 }
47  
48 #########
49 # RGB Functions
50 #
51 sub rgb {
52 1 ▶ my ($self, $r, $g, $b) = @_;
Subroutine rgb: called CC=1 SCAR=0.0
Pod documented
53  
54 1 my $cc = Convert::Color->new( "rgb8:$r,$g,$b" );
55  
56 1 return q[#].$cc->as_rgb8->hex;
57 }
58  
59 # TODO: rgba
60  
61 sub red {
62 1 ▶ my ($self, $color) = @_;
Subroutine red: called CC=1 SCAR=0.0
Pod documented
63 1 return $self->_color($color)->as_rgb8->red;
64 }
65  
66 sub green {
67 1 ▶ my ($self, $color) = @_;
Subroutine green: called CC=1 SCAR=0.0
Pod documented
68 1 return $self->_color($color)->as_rgb8->green;
69 }
70  
71 sub blue {
72 1 ▶ my ($self, $color) = @_;
Subroutine blue: called CC=1 SCAR=0.0
Pod documented
73 1 return $self->_color($color)->as_rgb8->blue;
74 }
75  
76 sub mix {
77 2 ▶ my ($self, $c1, $c2, $w) = @_;
Subroutine mix: called CC=2 SCAR=6.9
Pod documented
78  
79 # TODO: Weight not supported
80 2 ▶ $w ||= '50%';
Condition100%2/2
$w ||= "50%"
l!l
11
MC/DC100%1/1
$w ||= "50%"
$w
Truth table100%2/2
A
A= $w
Aresult
11
00
81  
82 2 $c1 = $self->_color($c1);
83 2 $c2 = $self->_color($c2);
84 2 $w = $self->_value($w);
85 2 my $w2 = 1-$w;
86  
87 2 my $r = int(($c1->as_rgb8->red * $w) + ($c2->as_rgb8->red * $w2)) ;
88 2 my $g = int(($c1->as_rgb8->green * $w) + ($c2->as_rgb8->green * $w2)) ;
89 2 my $b = int(($c1->as_rgb8->blue * $w) + ($c2->as_rgb8->blue * $w2)) ;
90  
91 2 return q[#].Convert::Color->new("rgb8:$r,$g,$b")->hex;
92 }
93  
94 #########
95 # HSL functions
96 #
97 sub hsl {
98 1 ▶ my ($self, $h, $s, $l) = @_;
Subroutine hsl: called CC=1 SCAR=0.0
Pod documented
99  
100 1 $s = $self->_value($s);
101 1 $l = $self->_value($l);
102 1 my $cc = Convert::Color->new( "hsl:$h,$s,$l" );
103  
104 1 return q[#].$cc->as_rgb8->hex;
105 }
106  
107 # TODO: hsla
108  
109 sub hue {
110 1 ▶ my ($self, $color) = @_;
Subroutine hue: called CC=1 SCAR=0.0
Pod documented
111 1 return $self->_color($color)->as_hsl->hue;
112 }
113  
114 sub saturation {
115 1 ▶ my ($self, $color) = @_;
Subroutine saturation: called CC=1 SCAR=0.0
Pod documented
116 1 return $self->_color($color)->as_hsl->saturation;
117 }
118  
119 sub lightness {
120 1 ▶ my ($self, $color) = @_;
Subroutine lightness: called CC=1 SCAR=0.0
Pod documented
121 1 return $self->_color($color)->as_hsl->lightness;
122 }
123  
124 sub adjust_hue {
125 3 ▶ my ($self, $color, $value) = @_;
Subroutine adjust_hue: called CC=1 SCAR=0.0
Pod documented
126  
127 3 my $cc = $self->_color($color);
128 3 my $hsl = $cc->as_hsl;
129 3 my $new_hsl = Convert::Color->new( sprintf q[hsl:%s,%s,%s],
130 $hsl->hue+$value,
131 $hsl->saturation,
132 $hsl->lightness );
133  
134 3 return q[#].$new_hsl->as_rgb8->hex;
135 }
136  
137 sub lighten {
138 1 ▶ my ($self, $color, $value) = @_;
Subroutine lighten: called CC=1 SCAR=0.0
Pod documented
139  
140 1 $value = $self->_value($value);
141 1 my $cc = $self->_color($color);
142 1 my $hsl = $cc->as_hsl;
143 1 my $new_hsl = Convert::Color->new( sprintf q[hsl:%s,%s,%s],
144 $hsl->hue,
145 $hsl->saturation,
146 $hsl->lightness+$value );
147  
148 1 return q[#].$new_hsl->as_rgb8->hex;
149 }
150  
151 sub darken {
152 7 ▶ my ($self, $color, $value) = @_;
Subroutine darken: called CC=1 SCAR=0.0
Pod documented
153  
154 7 $value = $self->_value($value);
155 7 my $cc = $self->_color($color);
156 7 my $hsl = $cc->as_hsl;
157 7 my $new_hsl = Convert::Color->new( sprintf q[hsl:%s,%s,%s],
158 $hsl->hue,
159 $hsl->saturation,
160 $hsl->lightness-$value );
161  
162 7 return q[#].$new_hsl->as_rgb8->hex;
163 }
164  
165 sub saturate {
166 1 ▶ my ($self, $color, $value) = @_;
Subroutine saturate: called CC=1 SCAR=0.0
Pod documented
167  
168 1 $value = $self->_value($value);
169 1 my $cc = $self->_color($color);
170 1 my $hsl = $cc->as_hsl;
171 1 my $new_hsl = Convert::Color->new( sprintf q[hsl:%s,%s,%s],
172 $hsl->hue,
173 $hsl->saturation+$value,
174 $hsl->lightness );
175  
176 1 return q[#].$new_hsl->as_rgb8->hex;
177 }
178  
179 sub desaturate {
180 3 ▶ my ($self, $color, $value) = @_;
Subroutine desaturate: called CC=2 SCAR=6.9
Pod documented
181  
182 3 $value = $self->_value($value);
183 3 my $cc = $self->_color($color);
184 3 my $hsl = $cc->as_hsl;
185 3 my $sat = ($hsl->saturation-$value);
186  
187 3 ▶ if ($sat < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sat < 0) 2 1 3
188 2 $sat = 0;
189 }
190  
191 3 my $new_hsl = Convert::Color->new( sprintf q[hsl:%s,%s,%s],
192 $hsl->hue,
193 $sat,
194 $hsl->lightness );
195  
196 3 return q[#].$new_hsl->as_rgb8->hex;
197 }
198  
199 sub grayscale {
200 1 ▶ my ($self, $color) = @_;
Subroutine grayscale: called CC=1 SCAR=0.0
Pod documented
201  
202 1 return $self->desaturate($color, "$PERC%");
203 }
204  
205 sub complement {
206 1 ▶ my ($self, $color) = @_;
Subroutine complement: called CC=1 SCAR=0.0
Pod documented
207  
208 1 Readonly::Scalar my $COMP_DEGREES => 180;
209 1 return $self->adjust_hue($color, $COMP_DEGREES);
210 }
211  
212 #########
213 # STRING Functions
214 #
215 sub unquote {
216 2 ▶ my ($self, $str) = @_;
Subroutine unquote: called CC=1 SCAR=0.0
Pod documented
217  
218 2 $str =~ s/^\"(.*)\"/$1/xms;
219 2 $str =~ s/^\'(.*)\'/$1/xms;
220  
221 2 return $str;
222 }
223  
224 sub quote {
225 4 ▶ my ($self, $str) = @_;
Subroutine quote: called CC=3 SCAR=11.2
Pod documented
226  
227 4 ▶ if ($str =~ /^\"(.*)\"/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\"(.*)\"/msx) 1 3 4
228 1 return $str;
229 }
230  
231 3 ▶ if ($str =~ /^\'(.*)\'/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\'(.*)\'/msx) 0 3 3
232 0 return $str;
233 }
234  
235 3 return qq["$str"];
236 }
237  
238 # NUMBER functions
239  
240 sub percentage {
241 1 ▶ my ($self, $num) = @_;
Subroutine percentage: called CC=1 SCAR=0.0
Pod documented
242  
243 1 return ($num * $PERC) . q[%];
244 }
245  
246 sub round {
247 2 ▶ my ($self, $str) = @_;
Subroutine round: called CC=1 SCAR=0.0
Pod documented
248  
249 2 my $num = Text::Sass::Expr->units($str);
250 2 return sprintf q[%.0f%s], $num->[0], $num->[1];
251 }
252  
253 sub ceil {
254 2 ▶ my ($self, $str) = @_;
Subroutine ceil: called CC=1 SCAR=0.0
Pod documented
255  
256 2 my $num = Text::Sass::Expr->units($str);
257 2 return POSIX::ceil($num->[0]).$num->[1];
258 }
259  
260 sub floor {
261 3 ▶ my ($self, $str) = @_;
Subroutine floor: called CC=1 SCAR=0.0
Pod documented
262  
263 3 my $num = Text::Sass::Expr->units($str);
264 3 return POSIX::floor($num->[0]).$num->[1];
265 }
266  
267 sub abs { ## no critic (Homonym)
268 2 ▶ my ($self, $str) = @_;
Subroutine abs: called CC=1 SCAR=0.0
Pod documented
269 2 my $num = Text::Sass::Expr->units($str);
270  
271 2 return POSIX::abs($num->[0]).$num->[1];
272 }
273  
274 #########
275 # Introspective functions
276 #
277 sub unit {
278 3 ▶ my ($self, $str) = @_;
Subroutine unit: called CC=1 SCAR=0.0
Pod documented
279  
280 3 my $num = Text::Sass::Expr->units($str);
281 3 return q["].$num->[1].q["];
282 }
283  
284 sub unitless {
285 2 ▶ my ($self, $str) = @_;
Subroutine unitless: called CC=2 SCAR=6.9
Pod documented
286  
287 2 my $num = Text::Sass::Expr->units($str);
288 2 ▶ return $num->[1] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$num->[1] ? : 1 1 2
289 }
290  
291 30 1;
292  
293 __END__
« blib/script/sass2css.pl ↑ Summary blib/lib/Text/Sass/Token.pm »