| 1 | package Graphics::Colourset; | ||
| 2 | |||
| 3 | # vim:ts=8 sw=4 sts=4 ai | ||
| 4 | |||
| 5 | our $VERSION = '0.07'; # VERSION | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Graphics::Colourset - create sets of colours. | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | version 0.07 | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Graphics::Colourset; | ||
| 18 | |||
| 19 | my $cs1 = Graphics::Colourset->new(hue=>60, shade=>1); | ||
| 20 | my @colsets = Graphics::Colourset->make_n_coloursets(number=>3, | ||
| 21 | hues=>\@hues,shades=>\@shades); | ||
| 22 | |||
| 23 | my $col_str = $cs1->as_hex_string('foreground'); | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This module generates the colour definitions of a set of colours | ||
| 28 | suitable for using as the basis of a colour-scheme for an X-Windows | ||
| 29 | window-manager. They can also be used for CSS colour descriptions for | ||
| 30 | Web-pages. The colours are defined as the traditional "hex string", | ||
| 31 | or as the more recent "rgb string". | ||
| 32 | |||
| 33 | The aim of this is to avoid having to generate harmonious colour schemes | ||
| 34 | by hand but to input a minimum number of parameters and to create all | ||
| 35 | the colours from that. | ||
| 36 | |||
| 37 | =head1 DETAILS | ||
| 38 | |||
| 39 | =head2 Coloursets | ||
| 40 | |||
| 41 | A "colourset" is a set of five colours, suitable for defining one type | ||
| 42 | of component in a window-manager or web-site "theme" or "colour scheme". | ||
| 43 | All colours in a colourset have the same hue, but have different | ||
| 44 | saturation and value (different "strengths") in keeping with their | ||
| 45 | different roles. | ||
| 46 | |||
| 47 | They are oriented towards being used for generating colours for buttons | ||
| 48 | and borders. | ||
| 49 | |||
| 50 | =over | ||
| 51 | |||
| 52 | =item background | ||
| 53 | |||
| 54 | The background colour is the main colour of the colourset, to be used | ||
| 55 | for the background of the "component" (whatever that may be). | ||
| 56 | |||
| 57 | =item topshadow | ||
| 58 | |||
| 59 | The topshadow colour is a colour slightly lighter than the background | ||
| 60 | colour, suitable for using to define a "top shadow" colour. | ||
| 61 | |||
| 62 | =item bottomshadow | ||
| 63 | |||
| 64 | The bottomshadow colour is a colour slightly darker than the background | ||
| 65 | colour, suitable for using to define a "bottom shadow" colour. | ||
| 66 | |||
| 67 | =item foreground | ||
| 68 | |||
| 69 | The foreground colour is the colour designated to be used for the | ||
| 70 | foreground, for text and the like. It is either much lighter or much | ||
| 71 | darker than the background colour, in order to contrast suitably. | ||
| 72 | |||
| 73 | =item foreground_inactive | ||
| 74 | |||
| 75 | The "inactive" foreground colour is a colour which is intended to be | ||
| 76 | used for things which are "greyed out", or not active. It is a colour | ||
| 77 | which contrasts with the background, but not as much as the "foreground" | ||
| 78 | colour. | ||
| 79 | |||
| 80 | =item foreground_high | ||
| 81 | |||
| 82 | The "high" foreground colour is a colour which is intended to be used for | ||
| 83 | alerts that something is in the "high" range, whether that be CPU usage or | ||
| 84 | weather temperature. It is a colour which is more red than the foreground | ||
| 85 | colour. | ||
| 86 | |||
| 87 | =item foreground_low | ||
| 88 | |||
| 89 | The "low" foreground colour is a colour which is intended to be used for | ||
| 90 | alerts that something is in the "low" range, whether that be CPU usage or | ||
| 91 | weather temperature. It is a colour which is more blue than the foreground | ||
| 92 | colour. | ||
| 93 | |||
| 94 | =back | ||
| 95 | |||
| 96 | There are two parameters which determine the colours of a colourset. | ||
| 97 | |||
| 98 | =over | ||
| 99 | |||
| 100 | =item hue | ||
| 101 | |||
| 102 | The hue in a 360 degree colour wheel. As a special tweak, if the hue | ||
| 103 | equals 360, it is taken to be no hue at all (grey). This doesn't | ||
| 104 | actually lose any hues, since 360 is normally exactly the same as zero | ||
| 105 | (red). | ||
| 106 | |||
| 107 | =item shade | ||
| 108 | |||
| 109 | The general "lightness" of the background. This is a range from 1 to 4, with 1 | ||
| 110 | being the darkest and 4 being the lightest. This also determines the | ||
| 111 | foreground colour, since a dark background will need a light foreground and | ||
| 112 | visa-versa. | ||
| 113 | |||
| 114 | If the shade is outside this range, a random shade will be picked. | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =head2 Base and Alternative Coloursets | ||
| 119 | |||
| 120 | The "base" colourset is considered to be the main colourset; additional | ||
| 121 | coloursets can be generated which are related to the base colourset in a | ||
| 122 | contrasting-but-harmonious way. | ||
| 123 | |||
| 124 | One test for the harmoniousness is to compare two coloursets and decide | ||
| 125 | whether they would be "ugly" together. This is done in a rule-of-thumb | ||
| 126 | way, which isn't perfect. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | 4 | ▶ | use common::sense; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 131 | 0 | ▶ | use Graphics::ColorObject; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 132 | |||
| 133 | =head1 CLASS METHODS | ||
| 134 | |||
| 135 | =head2 new | ||
| 136 | |||
| 137 | Create a new colourset, given an input hue, and foreground/background | ||
| 138 | disposition. | ||
| 139 | |||
| 140 | $my colset = Graphics::Colourset->new( | ||
| 141 | hue=>$hue, | ||
| 142 | shade=>1, | ||
| 143 | ); | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub new { | ||
| 148 | my $class = shift; | ||
| 149 | my %parameters = @_; | ||
| 150 | my $self = bless ({%parameters}, ref ($class) || $class); | ||
| 151 | $self->{hue} ||= 0; | ||
| 152 | $self->{shade} = 0 if !defined $self->{shade}; | ||
| 153 | |||
| 154 | if ($self->{shade} < 1 or $self->{shade} > 4) | ||
| 155 | { | ||
| 156 | $self->{shade} = int(rand(4)) + 1; | ||
| 157 | } | ||
| 158 | |||
| 159 | my $greener = $self->greener_hue(); | ||
| 160 | my $redder = $self->redder_hue(); | ||
| 161 | print STDERR "=> hue: $self->{hue}, low: $greener, high: $redder, shade: $self->{shade}\n"; | ||
| 162 | if ($self->{hue} == 360) # make it grey | ||
| 163 | { | ||
| 164 | if ($self->{shade} == 1) # darkest | ||
| 165 | { | ||
| 166 | $self->{foreground} = | ||
| 167 | Graphics::ColorObject->new_HSV([0, 0, 0.99]); | ||
| 168 | $self->{foreground_low} = | ||
| 169 | Graphics::ColorObject->new_HSV([$greener, 0.5, 0.99]); | ||
| 170 | $self->{foreground_high} = | ||
| 171 | Graphics::ColorObject->new_HSV([$redder, 0.5, 0.99]); | ||
| 172 | $self->{foreground_inactive} = | ||
| 173 | Graphics::ColorObject->new_HSV([0, 0, 0.70]); | ||
| 174 | $self->{background} = | ||
| 175 | Graphics::ColorObject->new_HSV([0, 0, 0.20]); | ||
| 176 | $self->{topshadow} = | ||
| 177 | Graphics::ColorObject->new_HSV([0, 0, 0.30]); | ||
| 178 | $self->{bottomshadow} = | ||
| 179 | Graphics::ColorObject->new_HSV([0, 0, 0.10]); | ||
| 180 | } | ||
| 181 | elsif ($self->{shade} == 2) | ||
| 182 | { | ||
| 183 | $self->{foreground} = | ||
| 184 | Graphics::ColorObject->new_HSV([0, 0, 0.95]); | ||
| 185 | $self->{foreground_low} = | ||
| 186 | Graphics::ColorObject->new_HSV([$greener, 0.5, 0.95]); | ||
| 187 | $self->{foreground_high} = | ||
| 188 | Graphics::ColorObject->new_HSV([$redder, 0.5, 0.95]); | ||
| 189 | $self->{foreground_inactive} = | ||
| 190 | Graphics::ColorObject->new_HSV([0, 0, 0.80]); | ||
| 191 | $self->{background} = | ||
| 192 | Graphics::ColorObject->new_HSV([0, 0, 0.50]); | ||
| 193 | $self->{topshadow} = | ||
| 194 | Graphics::ColorObject->new_HSV([0, 0, 0.70]); | ||
| 195 | $self->{bottomshadow} = | ||
| 196 | Graphics::ColorObject->new_HSV([0, 0, 0.30]); | ||
| 197 | } | ||
| 198 | elsif ($self->{shade} == 3) | ||
| 199 | { | ||
| 200 | $self->{foreground} = | ||
| 201 | Graphics::ColorObject->new_HSV([0, 0, 0.05]); | ||
| 202 | $self->{foreground_low} = | ||
| 203 | Graphics::ColorObject->new_HSV([$greener, 0.5, 0.05]); | ||
| 204 | $self->{foreground_high} = | ||
| 205 | Graphics::ColorObject->new_HSV([$redder, 0.5, 0.05]); | ||
| 206 | $self->{foreground_inactive} = | ||
| 207 | Graphics::ColorObject->new_HSV([0, 0, 0.60]); | ||
| 208 | $self->{background} = | ||
| 209 | Graphics::ColorObject->new_HSV([0, 0, 0.75]); | ||
| 210 | $self->{topshadow} = | ||
| 211 | Graphics::ColorObject->new_HSV([0, 0, 0.85]); | ||
| 212 | $self->{bottomshadow} = | ||
| 213 | Graphics::ColorObject->new_HSV([0, 0, 0.65]); | ||
| 214 | } | ||
| 215 | elsif ($self->{shade} == 4) # lightest | ||
| 216 | { | ||
| 217 | $self->{foreground} = | ||
| 218 | Graphics::ColorObject->new_HSV([0, 0, 0.20]); | ||
| 219 | $self->{foreground_low} = | ||
| 220 | Graphics::ColorObject->new_HSV([$greener, 0.5, 0.20]); | ||
| 221 | $self->{foreground_high} = | ||
| 222 | Graphics::ColorObject->new_HSV([$greener, 0.5, 0.20]); | ||
| 223 | $self->{foreground_inactive} = | ||
| 224 | Graphics::ColorObject->new_HSV([0, 0, 0.55]); | ||
| 225 | $self->{background} = | ||
| 226 | Graphics::ColorObject->new_HSV([0, 0, 0.88]); | ||
| 227 | $self->{topshadow} = | ||
| 228 | Graphics::ColorObject->new_HSV([0, 0, 0.96]); | ||
| 229 | $self->{bottomshadow} = | ||
| 230 | Graphics::ColorObject->new_HSV([0, 0, 0.78]); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | else # coloured | ||
| 234 | { | ||
| 235 | if ($self->{shade} == 1) # darkest | ||
| 236 | { | ||
| 237 | $self->{foreground} = | ||
| 238 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.10, 0.99]); | ||
| 239 | $self->{foreground_low} = | ||
| 240 | Graphics::ColorObject->new_HSV([$greener, 0.50, 0.99]); | ||
| 241 | $self->{foreground_high} = | ||
| 242 | Graphics::ColorObject->new_HSV([$redder, 0.50, 0.99]); | ||
| 243 | $self->{foreground_inactive} = | ||
| 244 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.30, 0.80]); | ||
| 245 | $self->{background} = | ||
| 246 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.90, 0.35]); | ||
| 247 | $self->{topshadow} = | ||
| 248 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.50, 0.45]); | ||
| 249 | $self->{bottomshadow} = | ||
| 250 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.90, 0.25]); | ||
| 251 | } | ||
| 252 | elsif ($self->{shade} == 2) | ||
| 253 | { | ||
| 254 | $self->{foreground} = | ||
| 255 | Graphics::ColorObject->new_HSV([$self->{hue}, 0, 0.99]); | ||
| 256 | $self->{foreground_low} = | ||
| 257 | Graphics::ColorObject->new_HSV([$greener, 0.3, 0.99]); | ||
| 258 | $self->{foreground_high} = | ||
| 259 | Graphics::ColorObject->new_HSV([$redder, 0.3, 0.99]); | ||
| 260 | $self->{foreground_inactive} = | ||
| 261 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.30, 0.90]); | ||
| 262 | $self->{background} = | ||
| 263 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.80, 0.50]); | ||
| 264 | $self->{topshadow} = | ||
| 265 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.50, 0.65]); | ||
| 266 | $self->{bottomshadow} = | ||
| 267 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.80, 0.45]); | ||
| 268 | } | ||
| 269 | elsif ($self->{shade} == 3) | ||
| 270 | { | ||
| 271 | if ($self->{hue} > 220 && $self->{hue} < 280) | ||
| 272 | { | ||
| 273 | # blue/purple are too dark to deal with this | ||
| 274 | $self->{foreground} = | ||
| 275 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.99, 0.05]); | ||
| 276 | $self->{foreground_low} = | ||
| 277 | Graphics::ColorObject->new_HSV([$greener, 0.99, 0.15]); | ||
| 278 | $self->{foreground_high} = | ||
| 279 | Graphics::ColorObject->new_HSV([$redder, 0.99, 0.15]); | ||
| 280 | $self->{foreground_inactive} = | ||
| 281 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.90, 0.60]); | ||
| 282 | $self->{background} = | ||
| 283 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.50, 0.85]); | ||
| 284 | $self->{topshadow} = | ||
| 285 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.50, 0.95]); | ||
| 286 | $self->{bottomshadow} = | ||
| 287 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.70, 0.75]); | ||
| 288 | } | ||
| 289 | else | ||
| 290 | { | ||
| 291 | $self->{foreground} = | ||
| 292 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.99, 0.05]); | ||
| 293 | $self->{foreground_low} = | ||
| 294 | Graphics::ColorObject->new_HSV([$greener, 0.99, 0.15]); | ||
| 295 | $self->{foreground_high} = | ||
| 296 | Graphics::ColorObject->new_HSV([$redder, 0.99, 0.15]); | ||
| 297 | $self->{foreground_inactive} = | ||
| 298 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.90, 0.60]); | ||
| 299 | $self->{background} = | ||
| 300 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.75, 0.75]); | ||
| 301 | $self->{topshadow} = | ||
| 302 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.45, 0.85]); | ||
| 303 | $self->{bottomshadow} = | ||
| 304 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.70, 0.65]); | ||
| 305 | } | ||
| 306 | } | ||
| 307 | elsif ($self->{shade} == 4) # lightest | ||
| 308 | { | ||
| 309 | $self->{foreground} = | ||
| 310 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.90, 0.20]); | ||
| 311 | $self->{foreground_low} = | ||
| 312 | Graphics::ColorObject->new_HSV([$greener, 0.99, 0.40]); | ||
| 313 | $self->{foreground_high} = | ||
| 314 | Graphics::ColorObject->new_HSV([$redder, 0.99, 0.40]); | ||
| 315 | $self->{foreground_inactive} = | ||
| 316 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.40, 0.55]); | ||
| 317 | $self->{background} = | ||
| 318 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.30, 0.92]); | ||
| 319 | $self->{topshadow} = | ||
| 320 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.20, 0.97]); | ||
| 321 | $self->{bottomshadow} = | ||
| 322 | Graphics::ColorObject->new_HSV([$self->{hue}, 0.40, 0.75]); | ||
| 323 | } | ||
| 324 | } | ||
| 325 | return ($self); | ||
| 326 | } # new | ||
| 327 | |||
| 328 | =head2 make_n_coloursets | ||
| 329 | |||
| 330 | my @colsets = Graphics::Colourset::make_n_coloursets(number=>$num, | ||
| 331 | shades=>[1,0,3,4], | ||
| 332 | hues=>[10,50,undef,undef], | ||
| 333 | styles=>\@styles); | ||
| 334 | |||
| 335 | Make $num coloursets, based on the given shades and hues; if a shade is | ||
| 336 | zero or undef, a random shade will be chosen; if a hue is undef, a random | ||
| 337 | hue will be chosen. The coloursets will be generated, but checked with | ||
| 338 | is_ugly to ensure that it isn't ugly. They will also be checked to make | ||
| 339 | sure that they aren't the same as the other coloursets. | ||
| 340 | |||
| 341 | styles (optional): The possible colourset styles. By default, this is | ||
| 342 | complement, splitcomp, triad, tetrad, analog, mono. | ||
| 343 | |||
| 344 | Note that larger numbers will take longer and be more difficult to generate. | ||
| 345 | |||
| 346 | =cut | ||
| 347 | sub make_n_coloursets { | ||
| 348 | my %args = ( | ||
| 349 | number=>1, | ||
| 350 | shades=>undef, | ||
| 351 | hues=>undef, | ||
| 352 | styles=>undef, | ||
| 353 | @_ | ||
| 354 | ); | ||
| 355 | |||
| 356 | my @colsets = (); | ||
| 357 | while (!@colsets) | ||
| 358 | { | ||
| 359 | @colsets = attempt_n_coloursets(%args); | ||
| 360 | } | ||
| 361 | return @colsets; | ||
| 362 | } # make_n_coloursets | ||
| 363 | |||
| 364 | =head2 attempt_n_coloursets | ||
| 365 | |||
| 366 | my @colsets = Graphics::Colourset::attempt_n_coloursets(number=>$num, | ||
| 367 | shades=>[1,0,3,4], | ||
| 368 | hues=>[10,50,undef,undef]); | ||
| 369 | |||
| 370 | Make $num coloursets, based on the given shades and hues; if a shade is | ||
| 371 | zero or undef, a random shade will be chosen; if a hue is undef, a random | ||
| 372 | hue will be chosen. The coloursets will be generated, but checked with | ||
| 373 | is_ugly to ensure that it isn't ugly. They will also be checked to make | ||
| 374 | sure that they aren't the same as the other coloursets. | ||
| 375 | |||
| 376 | If a colourset is ugly, an empty set is returned. | ||
| 377 | |||
| 378 | Note that larger numbers will take longer and be more difficult to generate. | ||
| 379 | |||
| 380 | =cut | ||
| 381 | sub attempt_n_coloursets { | ||
| 382 | my %args = ( | ||
| 383 | number=>1, | ||
| 384 | shades=>undef, | ||
| 385 | hues=>undef, | ||
| 386 | styles=>undef, | ||
| 387 | @_ | ||
| 388 | ); | ||
| 389 | |||
| 390 | my $num_colsets = $args{number}; | ||
| 391 | |||
| 392 | # set an array of shades; by default zero means random | ||
| 393 | my @shades = (); | ||
| 394 | for (my $i = 0; $i < $num_colsets; $i++) | ||
| 395 | { | ||
| 396 | $shades[$i] = 0; | ||
| 397 | } | ||
| 398 | # if shades are passed in, use them | ||
| 399 | if (defined $args{shades}) | ||
| 400 | { | ||
| 401 | for (my $i = 0; $i < @{$args{shades}}; $i++) | ||
| 402 | { | ||
| 403 | $shades[$i] = $args{shades}->[$i]; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | # set an array of hues; by default undefined means random | ||
| 407 | my @hues = (); | ||
| 408 | for (my $i = 0; $i < $num_colsets; $i++) | ||
| 409 | { | ||
| 410 | $hues[$i] = undef; | ||
| 411 | } | ||
| 412 | # if hues are passed in, use them | ||
| 413 | if (defined $args{hues}) | ||
| 414 | { | ||
| 415 | for (my $i = 0; $i < @{$args{hues}}; $i++) | ||
| 416 | { | ||
| 417 | $hues[$i] = $args{hues}->[$i]; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | # if styles are passed in, use them | ||
| 422 | my @styles = qw(complement splitcomp triad tetrad analog mono); | ||
| 423 | if (defined $args{styles}) | ||
| 424 | { | ||
| 425 | @styles = @{$args{styles}}; | ||
| 426 | } | ||
| 427 | my %intervals = ( | ||
| 428 | complement=>[0, 180], | ||
| 429 | splitcomp=>[0, 180-24, 180+24], | ||
| 430 | triad=>[0, 120, 240], | ||
| 431 | tetrad=>[0, 90, 180, 240], | ||
| 432 | analog=>[0, -30, 30, -60, 60], | ||
| 433 | mono=>[0, 0, 0, 0], | ||
| 434 | ); | ||
| 435 | |||
| 436 | my $style = $styles[int(rand(@styles))]; | ||
| 437 | my $num_intervals = @{$intervals{$style}}; | ||
| 438 | print STDERR "style:$style, num_intervals:$num_intervals\n"; | ||
| 439 | if ($style eq 'mono') | ||
| 440 | { | ||
| 441 | # set the shades for mono, dark to light | ||
| 442 | for (my $i = 0; $i < $num_colsets; $i++) | ||
| 443 | { | ||
| 444 | $shades[$i] = $i + 1; | ||
| 445 | } | ||
| 446 | } | ||
| 447 | |||
| 448 | my @colsets = (); | ||
| 449 | my $basehue = (defined $hues[0] ? $hues[0] : int(rand(360))); | ||
| 450 | |||
| 451 | while (@colsets < $num_colsets) | ||
| 452 | { | ||
| 453 | my $next_cs; | ||
| 454 | # get the index of the next colset | ||
| 455 | my $ind = @colsets; | ||
| 456 | $shades[$ind] = int(rand(4)) + 1 if $shades[$ind] == 0; | ||
| 457 | while ($ind >= $num_intervals | ||
| 458 | and $shades[$ind] == $shades[$ind - $num_intervals]) | ||
| 459 | { | ||
| 460 | $shades[$ind] = int(rand(4)) + 1; | ||
| 461 | } | ||
| 462 | my $shade = $shades[$ind]; | ||
| 463 | my $hue = $hues[$ind]; | ||
| 464 | if (!defined $hue) | ||
| 465 | { | ||
| 466 | if ($ind < $num_intervals) | ||
| 467 | { | ||
| 468 | $hue = $basehue + $intervals{$style}->[$ind]; | ||
| 469 | } | ||
| 470 | else | ||
| 471 | { | ||
| 472 | $hue = $basehue + $intervals{$style}->[$ind % | ||
| 473 | $num_intervals]; | ||
| 474 | } | ||
| 475 | $hue += 360 if ($hue < 0); | ||
| 476 | $hue -= 360 if ($hue > 360); | ||
| 477 | } | ||
| 478 | print STDERR "[$ind] "; | ||
| 479 | $next_cs = Graphics::Colourset->new(hue=>$hue, shade=>$shade); | ||
| 480 | if ($ind > 0 and $next_cs->is_ugly($colsets[$ind - 1])) | ||
| 481 | { | ||
| 482 | return (); | ||
| 483 | } | ||
| 484 | push @colsets, $next_cs; | ||
| 485 | } | ||
| 486 | |||
| 487 | return @colsets; | ||
| 488 | } # attempt_n_coloursets | ||
| 489 | |||
| 490 | =head1 OBJECT METHODS | ||
| 491 | |||
| 492 | =head2 as_hex_string | ||
| 493 | |||
| 494 | my $colstr = $self->as_hex_string('foreground'); | ||
| 495 | |||
| 496 | Return the given colour as a hex colour string | ||
| 497 | such as #99FF00 | ||
| 498 | |||
| 499 | =cut | ||
| 500 | |||
| 501 | sub as_hex_string { | ||
| 502 | my $self = shift; | ||
| 503 | my $colour = shift; | ||
| 504 | |||
| 505 | my ($r, $g, $b) = @{$self->{$colour}->as_RGB255()}; | ||
| 506 | return sprintf("#%02X%02X%02X", $r, $g, $b); | ||
| 507 | } # as_hex_string | ||
| 508 | |||
| 509 | =head2 as_rgb_string | ||
| 510 | |||
| 511 | my $colstr = $self->as_rgb_string('foreground'); | ||
| 512 | |||
| 513 | Return the given colour as an X colour string | ||
| 514 | such as rgb:99/FF/00 | ||
| 515 | |||
| 516 | =cut | ||
| 517 | |||
| 518 | sub as_rgb_string { | ||
| 519 | my $self = shift; | ||
| 520 | my $colour = shift; | ||
| 521 | |||
| 522 | my ($r, $g, $b) = @{$self->{$colour}->as_RGB255()}; | ||
| 523 | return sprintf("rgb:%02X/%02X/%02X", $r, $g, $b); | ||
| 524 | } # as_rgb_string | ||
| 525 | |||
| 526 | =head2 as_hsl_string | ||
| 527 | |||
| 528 | my $colstr = $self->as_hsl_string('foreground'); | ||
| 529 | |||
| 530 | Return the given colour as a colour string | ||
| 531 | such as hsl(0,100%,100%) | ||
| 532 | |||
| 533 | =cut | ||
| 534 | |||
| 535 | sub as_hsl_string { | ||
| 536 | my $self = shift; | ||
| 537 | my $colour = shift; | ||
| 538 | |||
| 539 | my ($h, $s, $l) = @{$self->{$colour}->as_HSL()}; | ||
| 540 | return sprintf("hsl(%d,%.0d%%,%.0d%%)", $h, $s * 100, $l * 100); | ||
| 541 | } # as_hsl_string | ||
| 542 | |||
| 543 | =head2 as_hsv | ||
| 544 | |||
| 545 | my ($h, $s, $v) = $self->as_hsv('foreground'); | ||
| 546 | |||
| 547 | Return the given colour an array of Hue, Saturation, Value | ||
| 548 | You can then do with the numbers as you will. | ||
| 549 | |||
| 550 | =cut | ||
| 551 | |||
| 552 | sub as_hsv { | ||
| 553 | my $self = shift; | ||
| 554 | my $colour = shift; | ||
| 555 | |||
| 556 | my ($h, $s, $v) = @{$self->{$colour}->as_HSV()}; | ||
| 557 | return ($h,$s,$v); | ||
| 558 | } # as_hsv | ||
| 559 | |||
| 560 | =head2 greener_hue | ||
| 561 | |||
| 562 | my $green = $self->greener_hue(); | ||
| 563 | |||
| 564 | Calculate a hue which is closer to green than the scheme's own hue | ||
| 565 | |||
| 566 | =cut | ||
| 567 | |||
| 568 | sub greener_hue { | ||
| 569 | my $self = shift; | ||
| 570 | |||
| 571 | my $greener = 120; | ||
| 572 | my $diff = 60; | ||
| 573 | if ($self->{hue} != 360) # not grey | ||
| 574 | { | ||
| 575 | $greener = ($self->{hue} + $diff) % 360; | ||
| 576 | } | ||
| 577 | if ($greener < 60 or $greener > 240) # redder than yellow, pinker than blue | ||
| 578 | { | ||
| 579 | $greener = ($self->{hue} - $diff) % 360; | ||
| 580 | if ($greener < 60 or $greener > 240) | ||
| 581 | { | ||
| 582 | $greener = 120; | ||
| 583 | } | ||
| 584 | } | ||
| 585 | return $greener; | ||
| 586 | } # greener_hue | ||
| 587 | |||
| 588 | =head2 redder_hue | ||
| 589 | |||
| 590 | my $red = $self->redder_hue(); | ||
| 591 | |||
| 592 | Calculate a hue which is closer to red than the scheme's own hue | ||
| 593 | |||
| 594 | =cut | ||
| 595 | |||
| 596 | sub redder_hue { | ||
| 597 | my $self = shift; | ||
| 598 | |||
| 599 | my $redder = 0; | ||
| 600 | my $diff = 60; | ||
| 601 | if ($self->{hue} != 360) # not grey | ||
| 602 | { | ||
| 603 | $redder = ($self->{hue} + 360 - $diff) % 360; | ||
| 604 | } | ||
| 605 | if ($redder > 50 and $redder < 270) # greener than yellow, bluer than purple | ||
| 606 | { | ||
| 607 | $redder = ($self->{hue} + 360 + $diff) % 360; | ||
| 608 | if ($redder > 50 and $redder < 270) | ||
| 609 | { | ||
| 610 | $redder = 0; | ||
| 611 | } | ||
| 612 | } | ||
| 613 | return $redder; | ||
| 614 | } # redder_hue | ||
| 615 | |||
| 616 | =head2 equals | ||
| 617 | |||
| 618 | Checks if the given colourset equals the passed-in one. | ||
| 619 | |||
| 620 | if ($colset->equals($other_colset)) | ||
| 621 | { | ||
| 622 | ... | ||
| 623 | } | ||
| 624 | |||
| 625 | =cut | ||
| 626 | sub equals { | ||
| 627 | my $self = shift; | ||
| 628 | my $colset2 = shift; | ||
| 629 | |||
| 630 | return ($self->{hue} == $colset2->{hue} | ||
| 631 | && $self->{shade} == $colset2->{shade}); | ||
| 632 | } # equals | ||
| 633 | |||
| 634 | =head2 is_ugly | ||
| 635 | |||
| 636 | my $ret = $colset1->is_ugly($colset2); | ||
| 637 | |||
| 638 | Compares two coloursets and declares whether they would be ugly | ||
| 639 | together. This is naturally a subjective assessment on the part of | ||
| 640 | the author, but hopefully helpful. | ||
| 641 | |||
| 642 | =cut | ||
| 643 | sub is_ugly { | ||
| 644 | my $colset1 = shift; | ||
| 645 | my $colset2 = shift; | ||
| 646 | |||
| 647 | if (($colset1->{hue} == 360 | ||
| 648 | && $colset2->{hue} >= 50 | ||
| 649 | && $colset2->{hue} <= 80) | ||
| 650 | || ($colset2->{hue} == 360 | ||
| 651 | && $colset1->{hue} >= 50 | ||
| 652 | && $colset1->{hue} <= 80)) | ||
| 653 | { | ||
| 654 | # yellow doesn't go with grey | ||
| 655 | return 1; | ||
| 656 | } | ||
| 657 | elsif (($colset1->{hue} == 360 | ||
| 658 | && $colset2->{hue} > 10 | ||
| 659 | && $colset2->{hue} < 50 | ||
| 660 | && $colset2->{shade} > 1) | ||
| 661 | || ($colset2->{hue} == 360 | ||
| 662 | && $colset1->{hue} > 10 | ||
| 663 | && $colset1->{hue} < 50 | ||
| 664 | && $colset1->{shade} > 1)) | ||
| 665 | { | ||
| 666 | # orange only looks good if it's dark | ||
| 667 | return 1; | ||
| 668 | } | ||
| 669 | elsif ($colset1->{hue} == 360 | ||
| 670 | || $colset2->{hue} == 360) | ||
| 671 | { | ||
| 672 | # everything else goes with grey | ||
| 673 | return 0; | ||
| 674 | } | ||
| 675 | # all colours within 30 degrees of each other look good | ||
| 676 | my $hdiff = abs($colset1->{hue} - $colset2->{hue}); | ||
| 677 | if ($hdiff <= 30) | ||
| 678 | { | ||
| 679 | return 0; | ||
| 680 | } | ||
| 681 | |||
| 682 | if (($colset1->{hue} >= 0 | ||
| 683 | && $colset1->{hue} < 10 | ||
| 684 | && $colset1->{shade} == 4 | ||
| 685 | && $colset2->{hue} >= 60 | ||
| 686 | && $colset2->{hue} < 70 | ||
| 687 | && $colset2->{shade} != 4) | ||
| 688 | || ($colset2->{hue} >= 0 | ||
| 689 | && $colset2->{hue} < 10 | ||
| 690 | && $colset2->{shade} == 4 | ||
| 691 | && $colset1->{hue} >= 60 | ||
| 692 | && $colset1->{hue} < 70 | ||
| 693 | && $colset1->{shade} != 4)) | ||
| 694 | { | ||
| 695 | # rose doesn't go with yellow or green | ||
| 696 | return 1; | ||
| 697 | } | ||
| 698 | if (($colset1->{hue} > 10 | ||
| 699 | && $colset1->{hue} <= 40 | ||
| 700 | && $colset1->{shade} > 1 | ||
| 701 | && $colset1->{shade} < 4 | ||
| 702 | && $colset2->{hue} > 60 | ||
| 703 | && $colset2->{hue} <= 100) | ||
| 704 | || ($colset2->{hue} > 10 | ||
| 705 | && $colset2->{hue} <= 40 | ||
| 706 | && $colset2->{shade} > 1 | ||
| 707 | && $colset2->{shade} < 4 | ||
| 708 | && $colset1->{hue} > 60 | ||
| 709 | && $colset1->{hue} <= 100)) | ||
| 710 | { | ||
| 711 | # orange doesn't go with green | ||
| 712 | return 1; | ||
| 713 | } | ||
| 714 | if (($colset1->{hue} >= 270 | ||
| 715 | && $colset1->{hue} < 280 | ||
| 716 | && $colset2->{hue} >= 330 | ||
| 717 | && $colset2->{hue} < 340 | ||
| 718 | && $colset2->{shade} > 1) | ||
| 719 | || ($colset2->{hue} >= 270 | ||
| 720 | && $colset2->{hue} < 280 | ||
| 721 | && $colset1->{hue} >= 330 | ||
| 722 | && $colset1->{hue} < 340 | ||
| 723 | && $colset1->{shade} > 1)) | ||
| 724 | { | ||
| 725 | # purple doesn't go with pinky-red | ||
| 726 | return 1; | ||
| 727 | } | ||
| 728 | if (($colset1->{hue} >= 280 | ||
| 729 | && $colset1->{hue} < 360 | ||
| 730 | && (($colset2->{hue} >= 340 | ||
| 731 | && $colset2->{hue} < 360) | ||
| 732 | || ($colset2->{hue} >= 0 | ||
| 733 | && $colset2->{hue} < 50)) | ||
| 734 | ) | ||
| 735 | || ($colset2->{hue} >= 280 | ||
| 736 | && $colset2->{hue} < 360 | ||
| 737 | && (($colset1->{hue} >= 340 | ||
| 738 | && $colset1->{hue} < 360) | ||
| 739 | || ($colset1->{hue} >= 0 | ||
| 740 | && $colset1->{hue} < 50)) | ||
| 741 | ) | ||
| 742 | ) | ||
| 743 | { | ||
| 744 | # violet doesn't go with pink/red | ||
| 745 | return 1; | ||
| 746 | } | ||
| 747 | if (($colset1->{hue} > 10 | ||
| 748 | && $colset1->{hue} <= 40 | ||
| 749 | && ($colset1->{shade} == 2 | ||
| 750 | || $colset1->{shade} == 3) | ||
| 751 | && $colset2->{hue} > 100 | ||
| 752 | && $colset2->{hue} <= 130) | ||
| 753 | || ($colset2->{hue} > 10 | ||
| 754 | && $colset2->{hue} <= 40 | ||
| 755 | && ($colset2->{shade} == 2 | ||
| 756 | || $colset2->{shade} == 3) | ||
| 757 | && $colset1->{hue} > 100 | ||
| 758 | && $colset1->{hue} <= 130)) | ||
| 759 | { | ||
| 760 | # orange doesn't go with green | ||
| 761 | return 1; | ||
| 762 | } | ||
| 763 | if (($colset1->{hue} >= 260 | ||
| 764 | && $colset1->{hue} < 280 | ||
| 765 | && (($colset2->{hue} >= 350 | ||
| 766 | && $colset2->{hue} < 360) | ||
| 767 | || ($colset2->{hue} >= 0 | ||
| 768 | && $colset2->{hue} <= 10))) | ||
| 769 | || ($colset2->{hue} >= 260 | ||
| 770 | && $colset2->{hue} < 280 | ||
| 771 | && (($colset1->{hue} >= 350 | ||
| 772 | && $colset1->{hue} < 360) | ||
| 773 | || ($colset1->{hue} >= 0 | ||
| 774 | && $colset1->{hue} <= 10)))) | ||
| 775 | { | ||
| 776 | # purple doesn't go with tomato-red or rose | ||
| 777 | return 1; | ||
| 778 | } | ||
| 779 | if (($colset1->{hue} >= 280 | ||
| 780 | && $colset1->{hue} < 350 | ||
| 781 | && $colset2->{hue} >= 10 | ||
| 782 | && $colset2->{hue} < 80) | ||
| 783 | || ($colset2->{hue} >= 280 | ||
| 784 | && $colset2->{hue} < 350 | ||
| 785 | && $colset1->{hue} >= 10 | ||
| 786 | && $colset1->{hue} < 80)) | ||
| 787 | { | ||
| 788 | # purple & pink don't go with orange, yellow or green | ||
| 789 | return 1; | ||
| 790 | } | ||
| 791 | if (($colset1->{hue} > 10 | ||
| 792 | && $colset1->{hue} < 90 | ||
| 793 | && $colset1->{shade} != 1 | ||
| 794 | && $colset2->{hue} > 130 | ||
| 795 | && $colset2->{hue} < 210 | ||
| 796 | && $colset2->{shade} != 1) | ||
| 797 | || ($colset2->{hue} > 10 | ||
| 798 | && $colset2->{hue} < 90 | ||
| 799 | && $colset2->{shade} != 1 | ||
| 800 | && $colset1->{hue} > 130 | ||
| 801 | && $colset1->{hue} < 210 | ||
| 802 | && $colset1->{shade} != 1)) | ||
| 803 | { | ||
| 804 | # orange & yellow don't go with green or cyan | ||
| 805 | return 1; | ||
| 806 | } | ||
| 807 | if (($colset1->{hue} > 50 | ||
| 808 | && $colset1->{hue} < 70 | ||
| 809 | && $colset1->{shade} == 1 | ||
| 810 | && $colset2->{hue} > 130 | ||
| 811 | && $colset2->{hue} < 210 | ||
| 812 | && $colset2->{shade} != 1) | ||
| 813 | || ($colset2->{hue} > 50 | ||
| 814 | && $colset2->{hue} < 70 | ||
| 815 | && $colset2->{shade} == 1 | ||
| 816 | && $colset1->{hue} > 130 | ||
| 817 | && $colset1->{hue} < 210 | ||
| 818 | && $colset1->{shade} != 1)) | ||
| 819 | { | ||
| 820 | # Khaki doesn't go with green or cyan | ||
| 821 | return 1; | ||
| 822 | } | ||
| 823 | if (($colset1->{hue} > 150 | ||
| 824 | && $colset1->{hue} < 200 | ||
| 825 | && $colset2->{hue} > 270 | ||
| 826 | && $colset2->{hue} < 320) | ||
| 827 | || ($colset2->{hue} > 150 | ||
| 828 | && $colset2->{hue} < 200 | ||
| 829 | && $colset1->{hue} > 270 | ||
| 830 | && $colset1->{hue} < 320)) | ||
| 831 | { | ||
| 832 | # turquoise/cyan doesn't go with orchid | ||
| 833 | return 1; | ||
| 834 | } | ||
| 835 | if (($colset1->{hue} > 240 | ||
| 836 | && $colset1->{hue} < 290 | ||
| 837 | && $colset2->{hue} > 0 | ||
| 838 | && $colset2->{hue} < 50) | ||
| 839 | || ($colset2->{hue} > 240 | ||
| 840 | && $colset2->{hue} < 290 | ||
| 841 | && $colset1->{hue} > 0 | ||
| 842 | && $colset1->{hue} < 50)) | ||
| 843 | { | ||
| 844 | # blue/purple doesn't go with orange | ||
| 845 | return 1; | ||
| 846 | } | ||
| 847 | if ($colset1->{hue} >= 290 | ||
| 848 | && $colset1->{hue} < 350 | ||
| 849 | && $colset2->{hue} >= 50 | ||
| 850 | && $colset2->{hue} < 110) | ||
| 851 | { | ||
| 852 | # violet/pink doesn't go with yellow/green | ||
| 853 | return 1; | ||
| 854 | } | ||
| 855 | |||
| 856 | # glary colour don't do well with dull or pale | ||
| 857 | # unless they're the same hue | ||
| 858 | if ((($colset1->{shade} == 3 | ||
| 859 | && ($colset1->{hue} < 200 | ||
| 860 | || $colset1->{hue} > 280) | ||
| 861 | && ($colset2->{shade} == 2 | ||
| 862 | || $colset2->{shade} == 4)) | ||
| 863 | || ($colset2->{shade} == 3 | ||
| 864 | && ($colset2->{hue} < 200 | ||
| 865 | || $colset2->{hue} > 280) | ||
| 866 | && ($colset1->{shade} == 2 | ||
| 867 | || $colset1->{shade} == 4))) | ||
| 868 | && $colset1->{hue} != $colset2->{hue}) | ||
| 869 | { | ||
| 870 | return 1; | ||
| 871 | } | ||
| 872 | # pink doesn't go with green or yellow, even though red does | ||
| 873 | if (($colset1->{hue} >= 0 | ||
| 874 | && $colset1->{hue} < 30 | ||
| 875 | && $colset1->{shade} == 4 | ||
| 876 | && $colset2->{hue} > 60 | ||
| 877 | && $colset2->{hue} <= 120 | ||
| 878 | && $colset2->{shade} != 4) | ||
| 879 | || ($colset2->{hue} >= 0 | ||
| 880 | && $colset2->{hue} < 30 | ||
| 881 | && $colset2->{shade} == 4 | ||
| 882 | && $colset1->{hue} > 60 | ||
| 883 | && $colset1->{hue} <= 120 | ||
| 884 | && $colset1->{shade} != 4)) | ||
| 885 | { | ||
| 886 | return 1; | ||
| 887 | } | ||
| 888 | # pale orange doesn't go with green | ||
| 889 | if (($colset1->{hue} >= 30 | ||
| 890 | && $colset1->{hue} < 50 | ||
| 891 | && $colset1->{shade} == 4 | ||
| 892 | && $colset2->{hue} > 90 | ||
| 893 | && $colset2->{hue} <= 130 | ||
| 894 | && $colset2->{shade} != 4) | ||
| 895 | || ($colset2->{hue} >= 30 | ||
| 896 | && $colset2->{hue} < 50 | ||
| 897 | && $colset2->{shade} == 4 | ||
| 898 | && $colset1->{hue} > 90 | ||
| 899 | && $colset1->{hue} <= 130 | ||
| 900 | && $colset1->{shade} != 4)) | ||
| 901 | { | ||
| 902 | return 1; | ||
| 903 | } | ||
| 904 | # glary red don't like khaki | ||
| 905 | if (($colset1->{hue} >= 0 | ||
| 906 | && $colset1->{hue} < 30 | ||
| 907 | && $colset1->{shade} == 3 | ||
| 908 | && $colset2->{hue} > 50 | ||
| 909 | && $colset2->{hue} < 70 | ||
| 910 | && $colset2->{shade} != 3) | ||
| 911 | || ($colset2->{hue} >= 0 | ||
| 912 | && $colset2->{hue} < 30 | ||
| 913 | && $colset2->{shade} == 3 | ||
| 914 | && $colset1->{hue} > 50 | ||
| 915 | && $colset1->{hue} < 70 | ||
| 916 | && $colset1->{shade} != 3)) | ||
| 917 | { | ||
| 918 | return 1; | ||
| 919 | } | ||
| 920 | |||
| 921 | return 0; | ||
| 922 | } # is_ugly | ||
| 923 | |||
| 924 | =head1 REQUIRES | ||
| 925 | |||
| 926 | Graphics::ColorObject | ||
| 927 | Getopt::Long | ||
| 928 | Getopt::ArgvFile | ||
| 929 | Pod::Usage | ||
| 930 | Test::More | ||
| 931 | |||
| 932 | =head1 INSTALLATION | ||
| 933 | |||
| 934 | To install this module, run the following commands: | ||
| 935 | |||
| 936 | perl Build.PL | ||
| 937 | ./Build | ||
| 938 | ./Build test | ||
| 939 | ./Build install | ||
| 940 | |||
| 941 | Or, if you're on a platform (like DOS or Windows) that doesn't like the | ||
| 942 | "./" notation, you can do this: | ||
| 943 | |||
| 944 | perl Build.PL | ||
| 945 | perl Build | ||
| 946 | perl Build test | ||
| 947 | perl Build install | ||
| 948 | |||
| 949 | In order to install somewhere other than the default, such as | ||
| 950 | in a directory under your home directory, like "/home/fred/perl" | ||
| 951 | go | ||
| 952 | |||
| 953 | perl Build.PL --install_base /home/fred/perl | ||
| 954 | |||
| 955 | as the first step instead. | ||
| 956 | |||
| 957 | This will install the files underneath /home/fred/perl. | ||
| 958 | |||
| 959 | You will then need to make sure that you alter the PERL5LIB variable to | ||
| 960 | find the modules, and the PATH variable to find the script. | ||
| 961 | |||
| 962 | Therefore you will need to change: | ||
| 963 | your path, to include /home/fred/perl/script (where the script will be) | ||
| 964 | |||
| 965 | PATH=/home/fred/perl/script:${PATH} | ||
| 966 | |||
| 967 | the PERL5LIB variable to add /home/fred/perl/lib | ||
| 968 | |||
| 969 | PERL5LIB=/home/fred/perl/lib:${PERL5LIB} | ||
| 970 | |||
| 971 | =head1 SEE ALSO | ||
| 972 | |||
| 973 | perl(1). | ||
| 974 | |||
| 975 | =head1 BUGS | ||
| 976 | |||
| 977 | Please report any bugs or feature requests to the author. | ||
| 978 | |||
| 979 | =head1 AUTHOR | ||
| 980 | |||
| 981 | Kathryn Andersen (RUBYKAT) | ||
| 982 | perlkat AT katspace dot com | ||
| 983 | http://www.katspace.com | ||
| 984 | |||
| 985 | =head1 COPYRIGHT AND LICENCE | ||
| 986 | |||
| 987 | Copyright (c) 2005 by Kathryn Andersen | ||
| 988 | |||
| 989 | This program is free software; you can redistribute it and/or modify it | ||
| 990 | under the same terms as Perl itself. | ||
| 991 | |||
| 992 | |||
| 993 | =cut | ||
| 994 | |||
| 995 | 1; # End of Graphics::Colourset | ||
| 996 | __END__ | ||