line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SHARYANTO::Color::Util; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
722
|
use 5.010001; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1351
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
#use List::Util qw(min); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require Exporter; |
10
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
12
|
|
|
|
|
|
|
mix_2_rgb_colors |
13
|
|
|
|
|
|
|
rand_rgb_color |
14
|
|
|
|
|
|
|
reverse_rgb_color |
15
|
|
|
|
|
|
|
rgb2grayscale |
16
|
|
|
|
|
|
|
rgb2sepia |
17
|
|
|
|
|
|
|
rgb_luminance |
18
|
|
|
|
|
|
|
tint_rgb_color |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '0.56'; # VERSION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub mix_2_rgb_colors { |
24
|
5
|
|
|
5
|
1
|
1506
|
my ($rgb1, $rgb2, $pct) = @_; |
25
|
|
|
|
|
|
|
|
26
|
5
|
|
100
|
|
|
17
|
$pct //= 0.5; |
27
|
|
|
|
|
|
|
|
28
|
5
|
50
|
|
|
|
29
|
$rgb1 =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
29
|
|
|
|
|
|
|
or die "Invalid rgb1 color, must be in 'ffffff' form"; |
30
|
5
|
|
|
|
|
13
|
my $r1 = hex($1); |
31
|
5
|
|
|
|
|
9
|
my $g1 = hex($2); |
32
|
5
|
|
|
|
|
10
|
my $b1 = hex($3); |
33
|
5
|
50
|
|
|
|
24
|
$rgb2 =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
34
|
|
|
|
|
|
|
or die "Invalid rgb2 color, must be in 'ffffff' form"; |
35
|
5
|
|
|
|
|
8
|
my $r2 = hex($1); |
36
|
5
|
|
|
|
|
8
|
my $g2 = hex($2); |
37
|
5
|
|
|
|
|
10
|
my $b2 = hex($3); |
38
|
|
|
|
|
|
|
|
39
|
5
|
|
|
|
|
47
|
return sprintf("%02x%02x%02x", |
40
|
|
|
|
|
|
|
$r1 + $pct*($r2-$r1), |
41
|
|
|
|
|
|
|
$g1 + $pct*($g2-$g1), |
42
|
|
|
|
|
|
|
$b1 + $pct*($b2-$b1), |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub rand_rgb_color { |
47
|
0
|
|
|
0
|
1
|
0
|
my ($rgb1, $rgb2) = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
0
|
|
|
0
|
$rgb1 //= '000000'; |
50
|
0
|
0
|
|
|
|
0
|
$rgb1 =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
51
|
|
|
|
|
|
|
or die "Invalid rgb1 color, must be in 'ffffff' form"; |
52
|
0
|
|
|
|
|
0
|
my $r1 = hex($1); |
53
|
0
|
|
|
|
|
0
|
my $g1 = hex($2); |
54
|
0
|
|
|
|
|
0
|
my $b1 = hex($3); |
55
|
0
|
|
0
|
|
|
0
|
$rgb2 //= 'ffffff'; |
56
|
0
|
0
|
|
|
|
0
|
$rgb2 =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
57
|
|
|
|
|
|
|
or die "Invalid rgb2 color, must be in 'ffffff' form"; |
58
|
0
|
|
|
|
|
0
|
my $r2 = hex($1); |
59
|
0
|
|
|
|
|
0
|
my $g2 = hex($2); |
60
|
0
|
|
|
|
|
0
|
my $b2 = hex($3); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
|
|
0
|
return sprintf("%02x%02x%02x", |
63
|
|
|
|
|
|
|
$r1 + rand()*($r2-$r1+1), |
64
|
|
|
|
|
|
|
$g1 + rand()*($g2-$g1+1), |
65
|
|
|
|
|
|
|
$b1 + rand()*($b2-$b1+1), |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub rgb2grayscale { |
70
|
1
|
|
|
1
|
1
|
3849
|
my ($rgb) = @_; |
71
|
|
|
|
|
|
|
|
72
|
1
|
50
|
|
|
|
11
|
$rgb =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
73
|
|
|
|
|
|
|
or die "Invalid rgb color, must be in 'ffffff' form"; |
74
|
1
|
|
|
|
|
4
|
my $r = hex($1); |
75
|
1
|
|
|
|
|
3
|
my $g = hex($2); |
76
|
1
|
|
|
|
|
3
|
my $b = hex($3); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
# basically we just average the R, G, B |
79
|
1
|
|
|
|
|
5
|
my $avg = int(($r + $g + $b)/3); |
80
|
1
|
|
|
|
|
10
|
return sprintf("%02x%02x%02x", $avg, $avg, $avg); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub rgb2sepia { |
84
|
1
|
|
|
1
|
1
|
1964
|
my ($rgb) = @_; |
85
|
|
|
|
|
|
|
|
86
|
1
|
50
|
|
|
|
10
|
$rgb =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
87
|
|
|
|
|
|
|
or die "Invalid rgb color, must be in 'ffffff' form"; |
88
|
1
|
|
|
|
|
4
|
my $r = hex($1); |
89
|
1
|
|
|
|
|
3
|
my $g = hex($2); |
90
|
1
|
|
|
|
|
3
|
my $b = hex($3); |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# reference: http://www.techrepublic.com/blog/howdoi/how-do-i-convert-images-to-grayscale-and-sepia-tone-using-c/120 |
93
|
1
|
|
|
|
|
5
|
my $or = ($r*0.393) + ($g*0.769) + ($b*0.189); |
94
|
1
|
|
|
|
|
3
|
my $og = ($r*0.349) + ($g*0.686) + ($b*0.168); |
95
|
1
|
|
|
|
|
3
|
my $ob = ($r*0.272) + ($g*0.534) + ($b*0.131); |
96
|
1
|
50
|
|
|
|
2
|
for ($or, $og, $ob) { $_ = 255 if $_ > 255 } |
|
3
|
|
|
|
|
9
|
|
97
|
1
|
|
|
|
|
8
|
return sprintf("%02x%02x%02x", $or, $og, $ob); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub reverse_rgb_color { |
101
|
1
|
|
|
1
|
1
|
1724
|
my ($rgb) = @_; |
102
|
|
|
|
|
|
|
|
103
|
1
|
50
|
|
|
|
9
|
$rgb =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
104
|
|
|
|
|
|
|
or die "Invalid rgb color, must be in 'ffffff' form"; |
105
|
1
|
|
|
|
|
5
|
my $r = hex($1); |
106
|
1
|
|
|
|
|
3
|
my $g = hex($2); |
107
|
1
|
|
|
|
|
2
|
my $b = hex($3); |
108
|
|
|
|
|
|
|
|
109
|
1
|
|
|
|
|
9
|
return sprintf("%02x%02x%02x", 255-$r, 255-$g, 255-$b); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub _rgb_luminance { |
113
|
8
|
|
|
8
|
|
28
|
my ($r, $g, $b) = @_; |
114
|
8
|
|
|
|
|
41
|
0.2126*$r/255 + 0.7152*$g/255 + 0.0722*$b/255; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub rgb_luminance { |
118
|
3
|
|
|
3
|
1
|
1539
|
my ($rgb) = @_; |
119
|
|
|
|
|
|
|
|
120
|
3
|
50
|
|
|
|
17
|
$rgb =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
121
|
|
|
|
|
|
|
or die "Invalid rgb color, must be in 'ffffff' form"; |
122
|
3
|
|
|
|
|
7
|
my $r = hex($1); |
123
|
3
|
|
|
|
|
6
|
my $g = hex($2); |
124
|
3
|
|
|
|
|
4
|
my $b = hex($3); |
125
|
|
|
|
|
|
|
|
126
|
3
|
|
|
|
|
11
|
return _rgb_luminance($r, $g, $b); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub tint_rgb_color { |
130
|
5
|
|
|
5
|
1
|
1763
|
my ($rgb1, $rgb2, $pct) = @_; |
131
|
|
|
|
|
|
|
|
132
|
5
|
|
100
|
|
|
17
|
$pct //= 0.5; |
133
|
|
|
|
|
|
|
|
134
|
5
|
50
|
|
|
|
27
|
$rgb1 =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
135
|
|
|
|
|
|
|
or die "Invalid rgb color, must be in 'ffffff' form"; |
136
|
5
|
|
|
|
|
13
|
my $r1 = hex($1); |
137
|
5
|
|
|
|
|
8
|
my $g1 = hex($2); |
138
|
5
|
|
|
|
|
7
|
my $b1 = hex($3); |
139
|
5
|
50
|
|
|
|
21
|
$rgb2 =~ /^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/o |
140
|
|
|
|
|
|
|
or die "Invalid tint color, must be in 'ffffff' form"; |
141
|
5
|
|
|
|
|
8
|
my $r2 = hex($1); |
142
|
5
|
|
|
|
|
7
|
my $g2 = hex($2); |
143
|
5
|
|
|
|
|
10
|
my $b2 = hex($3); |
144
|
|
|
|
|
|
|
|
145
|
5
|
|
|
|
|
11
|
my $lum = _rgb_luminance($r1, $g1, $b1); |
146
|
|
|
|
|
|
|
|
147
|
5
|
|
|
|
|
70
|
return sprintf("%02x%02x%02x", |
148
|
|
|
|
|
|
|
$r1 + $pct*($r2-$r1)*$lum, |
149
|
|
|
|
|
|
|
$g1 + $pct*($g2-$g1)*$lum, |
150
|
|
|
|
|
|
|
$b1 + $pct*($b2-$b1)*$lum, |
151
|
|
|
|
|
|
|
); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
# ABSTRACT: Color-related utilities |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |