1 package Data
::Faker
::Colour
;
6 use parent qw
/Data::Faker/;
7 use Convert
::Color
::HSLuv
;
9 our $VERSION = '0.001';
11 sub new
{ bless {}, shift } # Don't call superclass constructor
13 sub ir
($) { int rand $_[0] }
22 push @ret, [ir
256, ir
256, ir
256]
25 wantarray ?
@ret : $ret[0]
31 my ($cnt, $ch, $cs, $cl) = @_;
38 my ($h, $s, $l) = ($ch, $cs, $cl);
39 $h = rand 360 if $h < 0;
40 $s = rand 100 if $s < 0;
41 $l = rand 100 if $l < 0;
42 my @colour = Convert
::Color
::HSLuv
->new($h, $s, $l)->rgb;
51 wantarray ?
@ret : $ret[0]
56 sprintf "#%02x%02x%02x", @
$rgb
61 sprintf 'rgb(%d,%d,%d)', @
$rgb
64 sub colour_hex
{ map { to_hex
$_ } colour
@_ }
65 sub colour_css
{ map { to_css
$_ } colour
@_ }
66 sub colour_hsluv_hex
{ map { to_hex
$_ } colour_hsluv
@_ }
67 sub colour_hsluv_css
{ map { to_css
$_ } colour_hsluv
@_ }
71 *color_hsluv
= *colour_hsluv
;
72 *color_hex
= *colour_hex
;
73 *color_hsluv_hex
= *colour_hsluv_hex
;
74 *color_css
= *colour_css
;
75 *color_hsluv_css
= *colour_hsluv_css
;
77 for my $c (qw
/colour color/) {
78 __PACKAGE__
->register_plugin(
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
,
96 Data::Faker::Colour - Generate random colours
100 use Data::Faker::Colour;
103 my $f = Data::Faker::Colour->new;
104 say 'Random colour: ', $f->colour_hex;
105 say 'Three random colours of 60% lightness: ',
106 $f->colour_hsluv_hex(3, -1, -1, 60);
107 say 'A colour with 70% saturation, in CSS format: ',
108 $f->colour_hsluv_css(1, -1, 70);
109 say '5 colours with hue 120 and lightness 45%: ',
110 $f->colour_hsluv_hex(5, 150, -1, 45);
114 This module is a plugin for Data::Faker for generating random colours.
115 It uses the HSLuv colour space to permit generation of colours with
116 specific hue, saturation, or lightness values. One use case would be
117 generating colour schemes.
119 It is recommended to use this without Data::Faker, as Data::Faker does
120 not currently pass arguments to methods.
122 =head1 DATA PROVIDERS
126 =item B<colour>([I<$cnt>])
128 Generate I<$cnt> (default 1) random colours.
129 Returns a list of 3-element arrayrefs, representing the R, G, and B
130 components, each ranging 0-255.
132 =item B<colour_hex>([I<$cnt>])
134 As above, but returns a list of strings like C<#rrggbb>.
136 =item B<colour_css>([I<$cnt>])
138 As above, but returns a list of strings like C<rgb(r, g, b)>.
140 =item B<colour_hsluv>([I<$cnt>, I<$H>, I<$S>, I<$L>])
142 Generates I<$cnt> (default 1) random colours using the HSLuv colour
143 space. You can specify your desired hue, saturation and/or lightness,
144 and all generated colours will have that hue/saturation/lightness.
146 Set I<$H>, I<$S>, I<$L> to a positive value to request a specific
147 hue/saturation/lightness, or to -1 for a randomly chosen one. They all
150 =item B<colour_hsluv_hex>([I<$cnt>, I<$H>, I<$S>, I<$L>])
152 =item B<colour_hsluv_css>([I<$cnt>, I<$H>, I<$S>, I<$L>])
154 As above but with hex/css output.
158 C<color> can be substituted for C<colour> in any of the methods above.
162 L<Data::Faker>, L<Convert::Colour>, L<Convert::Colour::HSLuv>
166 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
168 =head1 COPYRIGHT AND LICENSE
170 Copyright (C) 2017 by Marius Gavrilescu
172 This library is free software; you can redistribute it and/or modify
173 it under the same terms as Perl itself, either Perl version 5.24.1 or,
174 at your option, any later version of Perl 5 you may have available.
This page took 0.030335 seconds and 4 git commands to generate.