Initial commit
[data-faker-colour.git] / lib / Data / Faker / Colour.pm
CommitLineData
f14ac6c3
MG
1package Data::Faker::Colour;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Data::Faker/;
7use Convert::Color::HSLuv;
8
9our $VERSION = '0.001';
10
11sub new { bless {}, shift } # Don't call superclass constructor
12
13sub ir ($) { int rand $_[0] }
14
15sub colour {
16 shift; # drop $self
17 my $cnt = shift // 1;
18 my %args = @_;
19 my @ret;
20
21 for (1 .. $cnt) {
22 push @ret, [ir 256, ir 256, ir 256]
23 }
24
25 wantarray ? @ret : $ret[0]
26}
27
28sub colour_hsluv {
29 shift; # drop $self
30 my @ret;
31 my ($cnt, $ch, $cs, $cl) = @_;
32 $cnt //= 1;
33 $ch //= -1;
34 $cs //= -1;
35 $cl //= -1;
36
37 for (1 .. $cnt) {
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;
43 for (@colour) {
44 $_ = int (256 * $_);
45 $_ = 0 if $_ < 0;
46 $_ = 255 if $_ > 255;
47 }
48 push @ret, \@colour
49 }
50
51 wantarray ? @ret : $ret[0]
52}
53
54sub to_hex {
55 my ($rgb) = @_;
56 sprintf "#%02x%02x%02x", @$rgb
57}
58
59sub to_css {
60 my ($rgb) = @_;
61 sprintf 'rgb(%d,%d,%d)', @$rgb
62}
63
64sub colour_hex { map { to_hex $_ } colour @_ }
65sub colour_css { map { to_css $_ } colour @_ }
66sub colour_hsluv_hex { map { to_hex $_ } colour_hsluv @_ }
67sub colour_hsluv_css { map { to_css $_ } colour_hsluv @_ }
68
69BEGIN {
70 *color = *colour;
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;
76
77 for my $c (qw/colour color/) {
78 __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
891;
90__END__
91
92=encoding utf-8
93
94=head1 NAME
95
96Data::Faker::Colour - Generate random colours
97
98=head1 SYNOPSIS
99
100 use Data::Faker::Colour;
101
102 local $, = ' ';
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);
111
112=head1 DESCRIPTION
113
114This module is a plugin for Data::Faker for generating random colours.
115It uses the HSLuv colour space to permit generation of colours with
116specific hue, saturation, or lightness values. One use case would be
117generating colour schemes.
118
119It is recommended to use this without Data::Faker, as Data::Faker does
120not currently pass arguments to methods.
121
122=head1 DATA PROVIDERS
123
124=over
125
126=item B<colour>([I<$cnt>])
127
128Generate I<$cnt> (default 1) random colours.
129Returns a list of 3-element arrayrefs, representing the R, G, and B
130components, each ranging 0-255.
131
132=item B<colour_hex>([I<$cnt>])
133
134As above, but returns a list of strings like C<#rrggbb>.
135
136=item B<colour_css>([I<$cnt>])
137
138As above, but returns a list of strings like C<rgb(r, g, b)>.
139
140=item B<colour_hsluv>([I<$cnt>, I<$H>, I<$S>, I<$L>])
141
142Generates I<$cnt> (default 1) random colours using the HSLuv colour
143space. You can specify your desired hue, saturation and/or lightness,
144and all generated colours will have that hue/saturation/lightness.
145
146Set I<$H>, I<$S>, I<$L> to a positive value to request a specific
147hue/saturation/lightness, or to -1 for a randomly chosen one. They all
148default to -1.
149
150=item B<colour_hsluv_hex>([I<$cnt>, I<$H>, I<$S>, I<$L>])
151
152=item B<colour_hsluv_css>([I<$cnt>, I<$H>, I<$S>, I<$L>])
153
154As above but with hex/css output.
155
156=back
157
158C<color> can be substituted for C<colour> in any of the methods above.
159
160=head1 SEE ALSO
161
162L<Data::Faker>, L<Convert::Colour>, L<Convert::Colour::HSLuv>
163
164=head1 AUTHOR
165
166Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
167
168=head1 COPYRIGHT AND LICENSE
169
170Copyright (C) 2017 by Marius Gavrilescu
171
172This library is free software; you can redistribute it and/or modify
173it under the same terms as Perl itself, either Perl version 5.24.1 or,
174at your option, any later version of Perl 5 you may have available.
175
176
177=cut
This page took 0.018416 seconds and 4 git commands to generate.