| 1 | use 5.14.0; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | use Moops; | ||
| 5 | |||
| 6 | # PODNAME: Types::CairoX::Sweet | ||
| 7 | |||
| 8 | library Types::CairoX::Sweet | ||
| 9 | |||
| 10 | extends Types::Standard, Types::TypeTiny | ||
| 11 | |||
| 12 | declares CairoImageSurface, | ||
| 13 | CairoContext, | ||
| 14 | ArrayRefNumOfTwo, | ||
| 15 | ArrayRefNumOfFour, | ||
| 16 | ArrayRefNumOfSix, | ||
| 17 | Color, | ||
| 18 | CurveTo, | ||
| 19 | LineTo, | ||
| 20 | MoveTo, | ||
| 21 | NumUpToOne, | ||
| 22 | Path, | ||
| 23 | Point | ||
| 24 | |||
| 25 | { | ||
| 26 | |||
| 27 | our $VERSION = '0.0108'; # VERSION | ||
| 28 | |||
| 29 | use List::AllUtils qw/any zip/; | ||
| 30 | |||
| 31 | class_type CairoContext => { class => 'Cairo::Context' }; | ||
| 32 | class_type CairoSurface => { class => 'Cairo::Surface' }; | ||
| 33 | |||
| 34 | class_type Color => { class => 'CairoX::Sweet::Color' }; | ||
| 35 | class_type CurveTo => { class => 'CairoX::Sweet::Core::CurveTo' }; | ||
| 36 | class_type LineTo => { class => 'CairoX::Sweet::Core::LineTo' }; | ||
| 37 | class_type MoveTo => { class => 'CairoX::Sweet::Core::MoveTo' }; | ||
| 38 | class_type Path => { class => 'CairoX::Sweet::Path' }; | ||
| 39 | class_type Point => { class => 'CairoX::Sweet::Core::Point' }; | ||
| 40 | |||
| 41 | declare ArrayRefNumOfTwo, as ArrayRef[Num], | ||
| 42 | where { scalar @$_ % 2 == 0 }, | ||
| 43 | message { | ||
| 44 | return ArrayRef->get_message($_) if !ArrayRef->check($_); | ||
| 45 | return "Takes two values per unit"; | ||
| 46 | }; | ||
| 47 | declare ArrayRefNumOfFour, as ArrayRef[Num], | ||
| 48 | where { scalar @$_ % 4 == 0 }, | ||
| 49 | message { | ||
| 50 | return ArrayRef->get_message($_) if !ArrayRef->check($_); | ||
| 51 | return "Takes four values per unit"; | ||
| 52 | }; | ||
| 53 | declare ArrayRefNumOfSix, as ArrayRef[Num], | ||
| 54 | where { scalar @$_ % 6 == 0 }, | ||
| 55 | message { | ||
| 56 | return ArrayRef->get_message($_) if !ArrayRef->check($_); | ||
| 57 | return "Takes six values per unit"; | ||
| 58 | }; | ||
| 59 | |||
| 60 | declare NumUpToOne, as Num, | ||
| 61 | where { $_ >= 0 && $_ <= 1 }, | ||
| 62 | message { | ||
| 63 | return Num->get_message($_) if !Num->check($_); | ||
| 64 | return "$_ is too small. Minimum allowed value is 0" if $_ < 0; | ||
| 65 | return "$_ is too big. Maximum allowed value is 1"; | ||
| 66 | }; | ||
| 67 | |||
| 68 | coerce Color, | ||
| 69 | from Str, via { | ||
| 70 | my $str = $_; | ||
| 71 | $str =~ s{^#}{}; | ||
| 72 | if($str =~ m{^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$}i) { | ||
| 73 | "CairoX::Sweet::Color"->new(red => (hex $1) / 255, green => (hex $2) / 255, blue => (hex $3) / 255 ); | ||
| 74 | } | ||
| 75 | }, | ||
| 76 | from ArrayRef, via { | ||
| 77 | my @array = @{ $_ }; | ||
| 78 | if(scalar @array == 3 || scalar @array == 4) { | ||
| 79 | my $opacity = scalar @array == 4 ? pop @array : undef; | ||
| 80 | |||
| 81 | my @colors = qw/red green blue/; | ||
| 82 | if(any { int $_ != $_ } @array) { | ||
| 83 | @colors = zip (@colors, @array); | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | @array = map { $_ / 255 } @array; | ||
| 87 | @colors = zip (@colors, @array); | ||
| 88 | } | ||
| 89 | my %color = @colors; | ||
| 90 | $color{'opacity'} = $opacity if defined $opacity; | ||
| 91 | |||
| 92 | "CairoX::Sweet::Color"->new(%color); | ||
| 93 | } | ||
| 94 | }; | ||
| 95 | |||
| 96 | coerce Point, | ||
| 97 | from ArrayRefNumOfTwo, via { | ||
| 98 | "CairoX::Sweet::Core::Point"->new($_->[0], $_->[1]); | ||
| 99 | }; | ||
| 100 | coerce MoveTo, | ||
| 101 | from ArrayRefNumOfTwo, via { | ||
| 102 | "CairoX::Sweet::Core::MoveTo"->new($_->[0], $_->[1]); | ||
| 103 | }; | ||
| 104 | } | ||
| 105 | |||
| 106 | 1; | ||
| 107 | |||
| 108 | __END__ | ||
| 109 | |||
| 110 | =pod | ||
| 111 | |||
| 112 | =encoding UTF-8 | ||
| 113 | |||
| 114 | =head1 NAME | ||
| 115 | |||
| 116 | Types::CairoX::Sweet | ||
| 117 | |||
| 118 | =head1 VERSION | ||
| 119 | |||
| 120 | Version 0.0108, released 2015-03-21. | ||
| 121 | |||
| 122 | =head1 SOURCE | ||
| 123 | |||
| 124 | L<https://github.com/Csson/p5-CairoX-Sweet> | ||
| 125 | |||
| 126 | =head1 HOMEPAGE | ||
| 127 | |||
| 128 | L<https://metacpan.org/release/CairoX-Sweet> | ||
| 129 | |||
| 130 | =head1 AUTHOR | ||
| 131 | |||
| 132 | Erik Carlsson <info@code301.com> | ||
| 133 | |||
| 134 | =head1 COPYRIGHT AND LICENSE | ||
| 135 | |||
| 136 | This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>. | ||
| 137 | |||
| 138 | This is free software; you can redistribute it and/or modify it under | ||
| 139 | the same terms as the Perl 5 programming language system itself. | ||
| 140 | |||
| 141 | =cut |