Perl 6 module for format conversion and math operations on colours
Perl6
Switch branches/tags
Nothing to show
Latest commit d47457e Feb 4, 2016 @zoffixznet Fix META
Permalink
Failed to load latest commit information.
examples First release Nov 17, 2015
lib Add .hex4 method (Closes #1) Dec 6, 2015
logotype First commit Nov 6, 2015
t Add .hex4 method (Closes #1) Dec 6, 2015
xt Fix packaging Feb 4, 2016
.gitignore Fix packaging Feb 4, 2016
.travis.yml First commit Nov 6, 2015
Changes Add .hex4 method (Closes #1) Dec 6, 2015
LICENSE First commit Nov 6, 2015
META6.json Fix META Feb 4, 2016
README.pod Add .hex4 method (Closes #1) Dec 6, 2015

README.pod

NAME

Color - Format conversion, manipulation, and math operations on colours

SYNOPSIS

use Color;
my $white        = Color.new(255, 255, 255);
my $almost_black = Color.new('#111');
say Color.new(:hsv<152 80 50>).hex; # convert HSV to HEX
say "$white is way lighter than $almost_black";

my $lighter_pink = Color.new('#ED60A2').lighten(20);
my $lighter_pink = Color.new('#ED60A2') ◐ 20; # same as above

my $saturated_pink = Color.new('#ED60A2').saturate(20);
my $saturated_pink = Color.new('#ED60A2') πŸž‰ 20; # same as above

# Create an inverted colour scheme:
$_ = .invert for @colours_in_my_colourscheme;

use Color::Operators; # this gives us some ops to use on Color objects
my $gray = $white / 2;
say $gray.hex; # prints "#808080"
say $almost_black + 25; # prints "42, 42, 42"

DESCRIPTION

This module allows you to perform mathematical operations on RGB colour tuples, as well as convert them into other colour formats, like hex, and manipulate them by, for example, making them lighter, darker, or more or less saturated.

OPERATORS

use Color;
use Color::Operators;

Note: as of this writing (Nov 17, 2015), merely importing the operators made Rakudo compile 20 seconds slower, hence the operators are in a separate module that you'll need to import.

CONSTRUCTOR

new

my $rgb = Color.new('abc');
Color.new('#abc');
Color.new('face');
Color.new('#face');
Color.new('abcdef');
Color.new('#abcdef');
Color.new('abcdefaa');
Color.new('#abcdefaa');
Color.new(:hex<abc>); # same applies to all other hex varians
Color.new( 255, 100, 25 ); # RGB
Color.new( .5, .1, .3, .4 ); # CMYK
Color.new( rgb => [ 255, 100, 25 ] );
Color.new(:rgb<255 100 25>); # same works on other formats
Color.new( rgbd => [.086, .165, .282] ); # decimal RGB
Color.new( rgba => [ 22, 42, 72, 88 ] );
Color.new( rgbad => [ .086, .165, .282, .345 ] );
Color.new( cmyk => [.55, .25, .85, .12] );
Color.new( hsl => [ 72, 78, 65] );
Color.new( hsv => [ 90, 60, 70] );

Creates new Color object. All of the above formats are supported. Note: internally, the color will be converted to RGBA, which might incurr slight precision loss when converting from other formats.

ATTRIBUTES

alpha-math

my $c = Color.new('abc');
$c.alpha-math = True;

my $c = Color.new('abca');
$c.alpha-math = False;

Boolean. Specifies whether operator math from Color::Operators should affect the alpha channel. Colours contructed from RGBA automatically get this attribute set to True, rest of formats have it set as False.

MANIPULATION METHODS

darken

say $c.darken(10).cmyk; # darken by 10%

Creates a new Color object that is darkened by the percetange given as the argument.

desaturate

say $c.desaturate(20).cmyk;

Creates a new Color object that is desaturated by the percetange given as the argument.

invert

say $c.invert.cmyk;

Creates a new Color object that is inverted (black becomes white, etc).

lighten

say $c.lighten(10).cmyk; # lighten by 10%

Creates a new Color object that is lightened by the percetange given as the argument.

saturate

say $c.saturate(20).cmyk;

Creates a new Color object that is saturated by the percetange given as the argument.

CONVERSION METHODS

to-string

$c.to-string('cmyk'); #   cmyk(0.954955, 0.153153, 0, 0.129412)
$c.to-string('hsl');  #   hsl(189.622642, 91.379310, 45.490196)
$c.to-string('hsv');  #   hsv(189.622642, 95.495495, 87.058824)
$c.to-string('rgb');  #   rgb(10, 188, 222)
$c.to-string('rgba'); #   rgba(10, 188, 222, 255)
$c.to-string('rgbd'); #   rgb(0.039216, 0.737255, 0.870588)
$c.to-string('rgbad');#   rgba(0.039216, 0.737255, 0.870588, 1)
$c.to-string('hex');  #   #0ABCDE
$c.to-string('hex3'); #   #1CE
$c.to-string('hex8'); #   #0ABCDEFF

Converts the color to the format given by the argument and returns a string representation of it. See above for the format of the tring for each color format.

Note: the .gist and .Str methods of the Color object are equivalent to .to-string('hex').

cmyk

say $c.cmyk; # (<106/111>, <17/111>, 0.0, <11/85>)

Converts the color to CMYK format and returns a list containing each color (ranging `0`..`1`).

hex

say $c.hex; #  (0A BC DE);

Returns a list of 3 2-digit hex numbers representing the color.

hex3

say $c.hex3; #  (1 C E);

Returns a list of 3 1-digit hex numbers representing the color. They will be rounded and they need to be doubled (i.e. the above would be 11CCEE) to get the actual value.

hex4

say $c.hex4; #  (1 C E F);

Returns a list of 4 1-digit hex numbers representing the color. They will be rounded and they need to be doubled (i.e. the above would be 11CCEEFF) to get the actual value.

hex8

say $c.hex8; #  (0A BC DE FF);

Returns a list of 4 2-digit hex numbers representing the color, including the Alpha space.

hsl

say $c.hsl; # (<10050/53>, <10600/111>, <1480/17>),

Converts the colour to HSL format and returns the three values (hue, saturation, lightness). The S/L are returned as percentages, not decimals, so 50% saturation is returned as 50, not .5.

hsv

say $c.hsv; # (<10050/53>, <10600/111>, <1480/17>),

Converts the colour to HSV format and returns the three values (hue, saturation, value). The S/V are returned as percentages, not decimals, so 50% saturation is returned as 50, not .5.

rgb

say $c.rgb; # (10, 188, 222)

Converts the color to RGB format and returns a list of the three colours.

rgba

say $c.rgba; # (10, 188, 222, 255);

Converts the color to RGBA format and returns a list of the three colours, and alpha channel.

rgbd

say $c.rgbd; # (<2/51>, <188/255>, <74/85>)

Converts the color to RGB format ranging 0..1 and returns a list of the three colours.

rgbad

say $c.rgbad; # (<2/51>, <188/255>, <74/85>, 1.0)

Converts the color to RGBA format ranging 0..1 and returns a list of the three colours, and alpha channel.

CUSTOM OPERATORS

To get these, you need to use Color::Operators along with use Color.

+

Color.new('424') + 10;
10 + Color.new('424');
Color.new('424') + Color.new('424');

Add individual RGB values of each color. Plain numbers are added to each value. If "alpha-math" is turned on, alpha channel is affected as well. The operation returns a new Color object.

-

Color.new('424') - 10;
10 - Color.new('424');
Color.new('424') - Color.new('666');

Subtract individual RGB values of each color. Plain numbers are subtracted from each value. If "alpha-math" is turned on, alpha channel is affected as well. The operation returns a new Color object.

*

Color.new('424') * 10;
10 * Color.new('424');
Color.new('424') * Color.new('424');

Multiply individual RGB values of each color. Plain numbers are multiplied with each value. If "alpha-math" is turned on, alpha channel is affected as well. The operation returns a new Color object.

/

Color.new('424') / 10;
Color.new('424') / 0; # doesn't die; sets values to 0
10 / Color.new('424');
Color.new('424') / Color.new('424');

Divide individual RGB values of each color. Plain numbers are divided with each value. If "alpha-math" is turned on, alpha channel is affected as well. The operation returns a new Color object. Illegal operation of division by zero, doesn't die and simply sets the value to 0.

◐

say $c ◐ 20; # lighten by 20%

U+25D0 (e2 97 90): CIRCLE WITH LEFT HALF BLACK [◐]. Same as /lighten

β—‘

say $c β—‘ 20; # darken by 20%

U+25D1 (e2 97 91): CIRCLE WITH RIGHT HALF BLACK [β—‘]. Same as /darken

πŸž‰

say $c πŸž‰ 20; # saturate by 20%

U+1F789 (f0 9f 9e 89): EXTREMELY HEAVY WHITE CIRCLE [πŸž‰]. Same as /desaturate

Β‘

say $cΒ‘; # invert colour

U+00A1 (c2 a1): INVERTED EXCLAMATION MARK [Β‘]. Same as /invert

STRINGIFICATION

say $c;
say "$c";

The Color object overrides .Str and .gist methods to be equivalent to .to-string('hex').

REPOSITORY

Fork this module on GitHub: https://github.com/zoffixznet/perl6-Color

BUGS

To report bugs or request features, please use https://github.com/zoffixznet/perl6-Color/issues

AUTHOR

Zoffix Znet http://zoffix.com/

CONTRIBUTORS

Thanks to timotimo++, jnthn++, psch++, RabidGravy++, ab5tract++, moritz++, and anyone else who I forgot who helped me with questions on IRC.

LICENSE

You can use and distribute this module under the same terms as Perl 6 itself. See the LICENSE file included in this distribution for complete details.