| 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 | 34 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 34 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 34 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 34 | ▶ | use Convert::Color; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 34 | ▶ | use Text::Sass::Expr; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 34 | ▶ | use POSIX qw(); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 34 | ▶ | use Readonly; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 34 | our $VERSION = q[1.1.0]; | |||||||||||||
| 19 | 34 | Readonly::Scalar my $PERC => 100; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 25 | ▶ | sub _color { | ||||||||||||
|
Subroutine
_color: called CC=2 SCAR=7.2
| |||||||||||||||
| 22 | 25 | my ($self, $color) = @_; | |||||||||||||
| 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
| |||||||||||||||
| 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 | 16 | ▶ | sub _value { | ||||||||||||
|
Subroutine
_value: called CC=3 SCAR=14.2
| |||||||||||||||
| 34 | 16 | my ($self, $value) = @_; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 16 | $value = Text::Sass::Expr->units($value); | |||||||||||||
| 37 | |||||||||||||||
| 38 | 16 | ▶ | if ($value->[1] eq q[%]) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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 | 1 | ▶ | sub rgb { | ||||||||||||
|
Subroutine
rgb: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 52 | 1 | my ($self, $r, $g, $b) = @_; | |||||||||||||
| 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 | 1 | ▶ | sub red { | ||||||||||||
|
Subroutine
red: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 62 | 1 | my ($self, $color) = @_; | |||||||||||||
| 63 | 1 | return $self->_color($color)->as_rgb8->red; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 1 | ▶ | sub green { | ||||||||||||
|
Subroutine
green: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 67 | 1 | my ($self, $color) = @_; | |||||||||||||
| 68 | 1 | return $self->_color($color)->as_rgb8->green; | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 1 | ▶ | sub blue { | ||||||||||||
|
Subroutine
blue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 72 | 1 | my ($self, $color) = @_; | |||||||||||||
| 73 | 1 | return $self->_color($color)->as_rgb8->blue; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 2 | ▶ | sub mix { | ||||||||||||
|
Subroutine
mix: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 77 | 2 | my ($self, $c1, $c2, $w) = @_; | |||||||||||||
| 78 | |||||||||||||||
| 79 | # TODO: Weight not supported | ||||||||||||||
| 80 | 2 | ▶ | $w ||= '50%'; | ||||||||||||
|
Condition100%2/2
$w ||= "50%"
MC/DC100%1/1
$w ||= "50%"
$w
Truth table100%2/2
A
A= $w
| |||||||||||||||
| 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 | 1 | ▶ | sub hsl { | ||||||||||||
|
Subroutine
hsl: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 98 | 1 | my ($self, $h, $s, $l) = @_; | |||||||||||||
| 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 | 1 | ▶ | sub hue { | ||||||||||||
|
Subroutine
hue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 110 | 1 | my ($self, $color) = @_; | |||||||||||||
| 111 | 1 | return $self->_color($color)->as_hsl->hue; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 1 | ▶ | sub saturation { | ||||||||||||
|
Subroutine
saturation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 115 | 1 | my ($self, $color) = @_; | |||||||||||||
| 116 | 1 | return $self->_color($color)->as_hsl->saturation; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 1 | ▶ | sub lightness { | ||||||||||||
|
Subroutine
lightness: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 120 | 1 | my ($self, $color) = @_; | |||||||||||||
| 121 | 1 | return $self->_color($color)->as_hsl->lightness; | |||||||||||||
| 122 | } | ||||||||||||||
| 123 | |||||||||||||||
| 124 | 3 | ▶ | sub adjust_hue { | ||||||||||||
|
Subroutine
adjust_hue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 125 | 3 | my ($self, $color, $value) = @_; | |||||||||||||
| 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 | 1 | ▶ | sub lighten { | ||||||||||||
|
Subroutine
lighten: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 138 | 1 | my ($self, $color, $value) = @_; | |||||||||||||
| 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 | 7 | ▶ | sub darken { | ||||||||||||
|
Subroutine
darken: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 152 | 7 | my ($self, $color, $value) = @_; | |||||||||||||
| 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 | 1 | ▶ | sub saturate { | ||||||||||||
|
Subroutine
saturate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 166 | 1 | my ($self, $color, $value) = @_; | |||||||||||||
| 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 | 3 | ▶ | sub desaturate { | ||||||||||||
|
Subroutine
desaturate: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 180 | 3 | my ($self, $color, $value) = @_; | |||||||||||||
| 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
| |||||||||||||||
| 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 | 1 | ▶ | sub grayscale { | ||||||||||||
|
Subroutine
grayscale: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 200 | 1 | my ($self, $color) = @_; | |||||||||||||
| 201 | |||||||||||||||
| 202 | 1 | return $self->desaturate($color, "$PERC%"); | |||||||||||||
| 203 | } | ||||||||||||||
| 204 | |||||||||||||||
| 205 | 1 | ▶ | sub complement { | ||||||||||||
|
Subroutine
complement: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 206 | 1 | my ($self, $color) = @_; | |||||||||||||
| 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 | 2 | ▶ | sub unquote { | ||||||||||||
|
Subroutine
unquote: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 216 | 2 | my ($self, $str) = @_; | |||||||||||||
| 217 | |||||||||||||||
| 218 | 2 | $str =~ s/^\"(.*)\"/$1/xms; | |||||||||||||
| 219 | 2 | $str =~ s/^\'(.*)\'/$1/xms; | |||||||||||||
| 220 | |||||||||||||||
| 221 | 2 | return $str; | |||||||||||||
| 222 | } | ||||||||||||||
| 223 | |||||||||||||||
| 224 | 4 | ▶ | sub quote { | ||||||||||||
|
Subroutine
quote: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 225 | 4 | my ($self, $str) = @_; | |||||||||||||
| 226 | |||||||||||||||
| 227 | 4 | ▶ | if ($str =~ /^\"(.*)\"/xms) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 228 | 1 | return $str; | |||||||||||||
| 229 | } | ||||||||||||||
| 230 | |||||||||||||||
| 231 | 3 | ▶ | if ($str =~ /^\'(.*)\'/xms) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 232 | 0 | return $str; | |||||||||||||
| 233 | } | ||||||||||||||
| 234 | |||||||||||||||
| 235 | 3 | return qq["$str"]; | |||||||||||||
| 236 | } | ||||||||||||||
| 237 | |||||||||||||||
| 238 | # NUMBER functions | ||||||||||||||
| 239 | |||||||||||||||
| 240 | 1 | ▶ | sub percentage { | ||||||||||||
|
Subroutine
percentage: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 241 | 1 | my ($self, $num) = @_; | |||||||||||||
| 242 | |||||||||||||||
| 243 | 1 | return ($num * $PERC) . q[%]; | |||||||||||||
| 244 | } | ||||||||||||||
| 245 | |||||||||||||||
| 246 | 2 | ▶ | sub round { | ||||||||||||
|
Subroutine
round: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 247 | 2 | my ($self, $str) = @_; | |||||||||||||
| 248 | |||||||||||||||
| 249 | 2 | my $num = Text::Sass::Expr->units($str); | |||||||||||||
| 250 | 2 | return sprintf q[%.0f%s], $num->[0], $num->[1]; | |||||||||||||
| 251 | } | ||||||||||||||
| 252 | |||||||||||||||
| 253 | 2 | ▶ | sub ceil { | ||||||||||||
|
Subroutine
ceil: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 254 | 2 | my ($self, $str) = @_; | |||||||||||||
| 255 | |||||||||||||||
| 256 | 2 | my $num = Text::Sass::Expr->units($str); | |||||||||||||
| 257 | 2 | return POSIX::ceil($num->[0]).$num->[1]; | |||||||||||||
| 258 | } | ||||||||||||||
| 259 | |||||||||||||||
| 260 | 3 | ▶ | sub floor { | ||||||||||||
|
Subroutine
floor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 261 | 3 | my ($self, $str) = @_; | |||||||||||||
| 262 | |||||||||||||||
| 263 | 3 | my $num = Text::Sass::Expr->units($str); | |||||||||||||
| 264 | 3 | return POSIX::floor($num->[0]).$num->[1]; | |||||||||||||
| 265 | } | ||||||||||||||
| 266 | |||||||||||||||
| 267 | 2 | ▶ | sub abs { ## no critic (Homonym) | ||||||||||||
|
Subroutine
abs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 268 | 2 | my ($self, $str) = @_; | |||||||||||||
| 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 | 3 | ▶ | sub unit { | ||||||||||||
|
Subroutine
unit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 278 | 3 | my ($self, $str) = @_; | |||||||||||||
| 279 | |||||||||||||||
| 280 | 3 | my $num = Text::Sass::Expr->units($str); | |||||||||||||
| 281 | 3 | return q["].$num->[1].q["]; | |||||||||||||
| 282 | } | ||||||||||||||
| 283 | |||||||||||||||
| 284 | 2 | ▶ | sub unitless { | ||||||||||||
|
Subroutine
unitless: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 285 | 2 | my ($self, $str) = @_; | |||||||||||||
| 286 | |||||||||||||||
| 287 | 2 | my $num = Text::Sass::Expr->units($str); | |||||||||||||
| 288 | 2 | ▶ | return $num->[1] ? 0 : 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 289 | } | ||||||||||||||
| 290 | |||||||||||||||
| 291 | 34 | 1; | |||||||||||||
| 292 | |||||||||||||||
| 293 | __END__ | ||||||||||||||