Quiq/Color.pm

filter: Statementstmt 92.6% 38 / 41 Branchbran 78.5% 11 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 91.7% 67 / 73 CC 9 SCAR 12.0
CC
9
Cov
89%
new
6.6
name
3.0
rgb
2.0
CRAP 3.3
« Quiq/ClassConfig.pm ↑ Summary Quiq/ClassLoader.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Color - Eine Farbe des RGB-Farbraums
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 SYNOPSIS
14  
15 use Quiq::Color;
16
17 # Instantiierung
18
19 $col = Quiq::Color->new(255,0,0); # aus dez. RGB-Tripel
20 $col = Quiq::Color->new('ff0000'); # aus hex. RGB-String
21
22 # Helligkeit
23
24 $brightness = $col->brightness; # 0 .. 255
25
26 # Farbname
27
28 $col->name('red'); # setzen
29 $name = $col->name; # abfragen
30
31 # Externe Repräsentation
32
33 $hex = $col->hexString; # 'ff0000' - hex. RGB-String
34 @rgb = $col->rgb; # (255,0,0) - dez. RGB-Tripel
35  
36 =head1 DESCRIPTION
37  
38 Ein Objekt der Klasse repräsentiert eine Farbe des RGB-Farbraums,
39 also ein Tripel (R, G, B). Das Objekt kann aus verschiedenen
40 externen Repräsentationen instantiiert werden und seinerseits
41 verschiedene externe Repräsentationen liefern. Ferner kann die
42 Helligkeit der Farbe ermittelt werden, was für eine Fontauswahl
43 nützlich sein kann. Außerdem kann der Farbe ein Name zugewiesen
44 werden.
45  
46 =cut
47  
48 # -----------------------------------------------------------------------------
49  
50 package Quiq::Color;
51 2 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 our $VERSION = '1.240';
58  
59 2 ▶use Quiq::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # -----------------------------------------------------------------------------
62  
63 =head1 METHODS
64  
65 =head2 Konstruktor
66  
67 =head3 new() - Konstruktor
68  
69 =head4 Synopsis
70  
71 $col = $class->new($r,$g,$b);
72 $col = $class->new(\@rgb);
73 $col = $class->new('rrggbb');
74 $col = $class->new('#rrggbb');
75  
76 =head4 Description
77  
78 Instantiiere eine RGB-Farbe und liefere eine Referenz
79 auf das Objekt zurück.
80  
81 =cut
82  
83 # -----------------------------------------------------------------------------
84  
85 11 ▶sub new {
Subroutine new: called CC=6 SCAR=18.9
Pod documented
86 11 my $class = shift;
87 # @_: Farbspezifikation
88  
89 11 my (@rgb,$error);
90 11 ▶ if (@_ == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 3) { } 5 6 11
elsif (@_ == 1) { } 6 0 6
91 5 @rgb = @_;
92 }
93 elsif (@_ == 1) {
94 6 ▶ if (Quiq::Reference->isArrayRef($_[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if ('Quiq::Reference'->isArrayRef($_[0])) { } 1 5 6
elsif ($_[0] =~ /^#?(\w\w)(\w\w)(\w\w)$/) { } 5 0 5
95 1 @rgb = @{$_[0]};
96 }
97 elsif ($_[0] =~ /^#?(\w\w)(\w\w)(\w\w)$/) {
98 5 @rgb = (hex($1),hex($2),hex($3));
99 }
100 else {
101 0 $error = 1;
102 }
103 }
104 else {
105 0 $error = 1;
106 }
107  
108 11 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 11 11
109 0 $class->throw(
110 'COLOR-00001: Unknown color representation',
111 Arguments => join(',',@_),
112 );
113 }
114  
115 # [$r,$g,$b],$name
116 11 return bless [\@rgb,undef],$class;
117 }
118  
119 # -----------------------------------------------------------------------------
120  
121 =head2 Farbeigenschaften
122  
123 =head3 brightness() - Helligkeit der Farbe
124  
125 =head4 Synopsis
126  
127 $brightness = $col->brightness;
128  
129 =head4 Description
130  
131 Liefere die Helligkeit der Farbe im Wertebereich 0 (schwarz) bis
132 255 (weiß). Bei einem Wert < 128 ist die Farbe dunkel, andernfalls hell.
133  
134 Die Methode kann genutzt werden um zu entscheiden, welche Textfarbe
135 auf einem Hintergrund mit der Farbe genutzt werden sollte.
136  
137 Die Helligkeitsberechnung erfolgt auf Grundlage der Heuristik:
138  
139 $brightness = sqrt 0.299*$r**2 + 0.587*$g**2 + 0.114*$b**2;
140  
141 =head4 See Also
142  
143 =over 2
144  
145 =item *
146  
147 L<http://fseitz.de/blog/index.php?/archives/112-Helligkeit-von-Farben-des-RGB-Farbraums-berechnen.html>
148  
149 =back
150  
151 =head4 Example
152  
153 $col = Quiq::Color->new('ff0000');
154 $brightness = $col->brightness;
155 -> 139.44
156  
157 =cut
158  
159 # -----------------------------------------------------------------------------
160  
161 3 ▶sub brightness {
Subroutine brightness: called CC=1 SCAR=0.0
Pod documented
162 3 my $self = shift;
163  
164 3 my ($r,$g,$b) = $self->rgb;
165 3 return sqrt 0.299*$r**2 + 0.587*$g**2 + 0.114*$b**2;
166 }
167  
168 # -----------------------------------------------------------------------------
169  
170 =head3 name() - Setze/Liefere Farbname
171  
172 =head4 Synopsis
173  
174 $name = $col->name($name);
175 $name = $col->name;
176  
177 =head4 Description
178  
179 Das Farbobjekt besitzt zunächst keinen Namen. Mit dieser Methode
180 kann der Farbe jedoch ein Name zugewiesen und abgefragt werden.
181  
182 Wurde dem Farbobjekt kein Name zugewiesen, liefert die Methode
183 einen Leerstring.
184  
185 =head4 Example
186  
187 my $col = Quiq::Color->new(255,0,0);
188 $col->name('red');
189 $name = $col->name;
190 -> 'red'
191  
192 =cut
193  
194 # -----------------------------------------------------------------------------
195  
196 3 ▶sub name {
Subroutine name: called CC=3 SCAR=11.0
Pod documented
197 3 my $self = shift;
198 # @_: $name
199  
200 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 2 3
201 1 $self->[1] = shift;
202 }
203  
204 3 ▶ return $self->[1] || '';
Condition100%2/2
$self->[1] || ''
l!l
21
MC/DC100%1/1
$self->[1] || ''
$self->[1]
Truth table100%2/2
A
A= $self->[1]
Aresult
11
00
205 }
206  
207 # -----------------------------------------------------------------------------
208  
209 =head2 Externe Repräsentationen
210  
211 =head3 hexString() - Farbwert als Hex-String
212  
213 =head4 Synopsis
214  
215 $hexStr = $col->hexString;
216  
217 =head4 Description
218  
219 Liefere den Farbwert der Farbe als Hex-String
220  
221 =head4 Example
222  
223 $col = Quiq::Color->new(255,255,255);
224 $hexStr = Quiq::Color->hexString;
225 -> 'ffffff'
226  
227 =cut
228  
229 # -----------------------------------------------------------------------------
230  
231 1 ▶sub hexString {
Subroutine hexString: called CC=1 SCAR=0.0
Pod documented
232 1 my $self = shift;
233 1 return sprintf '%02x%02x%02x',$self->rgb;
234 }
235  
236 # -----------------------------------------------------------------------------
237  
238 =head3 rgb() - Farbwert als Liste von dezimalen Werten
239  
240 =head4 Synopsis
241  
242 @rgb | $rgbA = $col->rgb;
243  
244 =head4 Description
245  
246 Liefere den Farbwert als Liste von dezimalen Werten mit den Komponenten
247 R, G, B. Im Skalarkontext liefere eine Referenz auf die Liste.
248  
249 =head4 Example
250  
251 $col = Quiq::Color->new('ffffff');
252 @rgb = Quiq::Color->rgb;
253 -> (255,255,255)
254  
255 =cut
256  
257 # -----------------------------------------------------------------------------
258  
259 6 ▶sub rgb {
Subroutine rgb: called CC=2 SCAR=6.9
Pod documented
260 6 my $self = shift;
261 6 ▶ return wantarray? @{$self->[0]}: $self->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 1 6
262 }
263  
264 # -----------------------------------------------------------------------------
265  
266 =head1 VERSION
267  
268 1.240
269  
270 =head1 AUTHOR
271  
272 Frank Seitz, L<http://fseitz.de/>
273  
274 =head1 COPYRIGHT
275  
276 Copyright (C) 2026 Frank Seitz
277  
278 =head1 LICENSE
279  
280 This code is free software; you can redistribute it and/or modify
281 it under the same terms as Perl itself.
282  
283 =cut
284  
285 # -----------------------------------------------------------------------------
286  
287 2 1;
288  
289 # eof
« Quiq/ClassConfig.pm ↑ Summary Quiq/ClassLoader.pm »