File Coverage

blib/lib/Data/Faker/Colour.pm
Criterion Covered Total %
statement 50 62 80.6
branch 11 14 78.5
condition 10 10 100.0
subroutine 10 16 62.5
pod 7 10 70.0
total 88 112 78.5


line stmt bran cond sub pod time code
1             package Data::Faker::Colour;
2              
3 1     1   13487 use 5.014000;
  1         4  
4 1     1   6 use strict;
  1         2  
  1         16  
5 1     1   4 use warnings;
  1         5  
  1         28  
6 1     1   356 use parent qw/Data::Faker/;
  1         221  
  1         4  
7 1     1   1416 use Convert::Color::HSLuv;
  1         34194  
  1         461  
8              
9             our $VERSION = '0.001';
10              
11 1     1 1 148 sub new { bless {}, shift } # Don't call superclass constructor
12              
13 18     18 0 90 sub ir ($) { int rand $_[0] }
14              
15             sub colour {
16 2     2 1 422 shift; # drop $self
17 2   100     10 my $cnt = shift // 1;
18 2         5 my %args = @_;
19 2         3 my @ret;
20              
21 2         7 for (1 .. $cnt) {
22 6         12 push @ret, [ir 256, ir 256, ir 256]
23             }
24              
25 2 50       13 wantarray ? @ret : $ret[0]
26             }
27              
28             sub colour_hsluv {
29 5     5 1 5117 shift; # drop $self
30 5         12 my @ret;
31 5         13 my ($cnt, $ch, $cs, $cl) = @_;
32 5   100     23 $cnt //= 1;
33 5   100     17 $ch //= -1;
34 5   100     25 $cs //= -1;
35 5   100     24 $cl //= -1;
36              
37 5         15 for (1 .. $cnt) {
38 2601         4802 my ($h, $s, $l) = ($ch, $cs, $cl);
39 2601 100       5794 $h = rand 360 if $h < 0;
40 2601 100       5234 $s = rand 100 if $s < 0;
41 2601 100       5217 $l = rand 100 if $l < 0;
42 2601         6326 my @colour = Convert::Color::HSLuv->new($h, $s, $l)->rgb;
43 2601         539413 for (@colour) {
44 7803         46293 $_ = int (256 * $_);
45 7803 50       15912 $_ = 0 if $_ < 0;
46 7803 100       15979 $_ = 255 if $_ > 255;
47             }
48 2601         5620 push @ret, \@colour
49             }
50              
51 5 50       258 wantarray ? @ret : $ret[0]
52             }
53              
54             sub to_hex {
55 0     0 0   my ($rgb) = @_;
56 0           sprintf "#%02x%02x%02x", @$rgb
57             }
58              
59             sub to_css {
60 0     0 0   my ($rgb) = @_;
61 0           sprintf 'rgb(%d,%d,%d)', @$rgb
62             }
63              
64 0     0 1   sub colour_hex { map { to_hex $_ } colour @_ }
  0            
65 0     0 1   sub colour_css { map { to_css $_ } colour @_ }
  0            
66 0     0 1   sub colour_hsluv_hex { map { to_hex $_ } colour_hsluv @_ }
  0            
67 0     0 1   sub colour_hsluv_css { map { to_css $_ } colour_hsluv @_ }
  0            
68              
69             BEGIN {
70 1     1   5 *color = *colour;
71 1         2 *color_hsluv = *colour_hsluv;
72 1         3 *color_hex = *colour_hex;
73 1         2 *color_hsluv_hex = *colour_hsluv_hex;
74 1         2 *color_css = *colour_css;
75 1         2 *color_hsluv_css = *colour_hsluv_css;
76              
77 1         3 for my $c (qw/colour color/) {
78 2         43 __PACKAGE__->register_plugin(
79             "${c}" => \&colour,
80             "${c}_hsluv" => \&colour_hsluv,
81             "${c}_hex" => \&colour_hex,
82             "${c}_hsluv_hex" => \&colour_hsluv_hex,
83             "${c}_css" => \&colour_css,
84             "${c}_hsluv_css" => \&colour_hsluv_css,
85             );
86             }
87             }
88              
89             1;
90             __END__