Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions lib/Color.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,83 @@ class Color:ver<1.004001>:auth<zef:raku-community-modules> {
has ValidRGB $.a = 255;
has Bool $.alpha-math is rw = False;

method h is rw {
Proxy.new:
FETCH => -> $ { self.hsv.head },
STORE => -> $, \h {
my $hsv = self.hsv;
$hsv[0] = h;

($!r, $!g, $!b) = (hsv2rgb $hsv)<r g b>».Int;
}
}

method s is rw {
Proxy.new:
FETCH => -> $ { self.hsv[1] },
STORE => -> $, \s {
my $hsv = self.hsv;
$hsv[1] = s;

($!r, $!g, $!b) = (hsv2rgb $hsv)<r g b>».Int;
}
}

method v is rw {
Proxy.new:
FETCH => -> $ { self.hsv.tail },
STORE => -> $, \v {
my $hsv = self.hsv;
$hsv[2] = v;

($!r, $!g, $!b) = (hsv2rgb $hsv)<r g b>».Int;
}
}

method c is rw {
Proxy.new:
FETCH => -> $ { self.cmyk.head },
STORE => -> $, \c {
my $cmyk = self.cmyk.Array;
$cmyk[0] = c;

($!r, $!g, $!b) = (cmyk2rgb $cmyk)<r g b>».Int;
}
}

method m is rw {
Proxy.new:
FETCH => -> $ { self.cmyk[1] },
STORE => -> $, \mg {
my $cmyk = self.cmyk.Array;
$cmyk[1] = mg;

($!r, $!g, $!b) = (cmyk2rgb $cmyk)<r g b>».Int;
}
}

method y is rw {
Proxy.new:
FETCH => -> $ { self.cmyk[2] },
STORE => -> $, \y {
my $cmyk = self.cmyk.Array;
$cmyk[2] = y;

($!r, $!g, $!b) = (cmyk2rgb $cmyk)<r g b>».Int;
}
}

method k is rw {
Proxy.new:
FETCH => -> $ { self.cmyk[3] },
STORE => -> $, \k {
my $cmyk = self.cmyk.Array;
$cmyk[3] = k;

($!r, $!g, $!b) = (cmyk2rgb $cmyk)<r g b>».Int;
}
}

##########################################################################
# Call forms
##########################################################################
Expand Down Expand Up @@ -129,6 +206,15 @@ class Color:ver<1.004001>:auth<zef:raku-community-modules> {
method rgbad() { $.r/255, $.g/255, $.b/255, $.a/255 }
method hex() { ($.r, $.g, $.b).map: *.fmt('%02X') }

method Int {
[+](
$.r +< 24,
$.g +< 16,
$.b +< 8,
$.a
);
}

method hex3() {
# Round the hex; the 247 bit is so we don't get hex 100 for high RGBs
($.r, $.g, $.b).map: {
Expand Down Expand Up @@ -707,6 +793,16 @@ say "$c";
The C<Color> object overrides C<.Str> and C<.gist> methods to be
equivalent to C<.to-string('hex')>.

=head1 Integer coercion

=begin code :lang<raku>

say $c.Int

=end code

Returns the integer representation of the Color in RGBA form.

=head1 Functional interface

The color conversion, manipulation and utility functions are defined within
Expand Down