| 1 |
NAME |
| 2 |
Color::RGB::Util - Utilities related to RGB colors |
| 3 |
|
| 4 |
VERSION |
| 5 |
This document describes version 0.609 of Color::RGB::Util (from Perl |
| 6 |
distribution Color-RGB-Util), released on 2025-03-19. |
| 7 |
|
| 8 |
SYNOPSIS |
| 9 |
use Color::RGB::Util qw( |
| 10 |
assign_rgb_color |
| 11 |
assign_rgb_dark_color |
| 12 |
assign_rgb_light_color |
| 13 |
int2rgb |
| 14 |
mix_2_rgb_colors |
| 15 |
mix_rgb_colors |
| 16 |
rand_rgb_color |
| 17 |
rand_rgb_colors |
| 18 |
reverse_rgb_color |
| 19 |
rgb2grayscale |
| 20 |
rgb2int |
| 21 |
rgb2sepia |
| 22 |
rgb_closest_to |
| 23 |
rgb_diff |
| 24 |
rgb_distance |
| 25 |
rgb_is_dark |
| 26 |
rgb_is_light |
| 27 |
rgb_luminance |
| 28 |
tint_rgb_color |
| 29 |
); |
| 30 |
|
| 31 |
say assign_rgb_color("foo"); # 0b5d33 |
| 32 |
say assign_rgb_dark_color("foo"); # 0b5d33 |
| 33 |
say assign_rgb_light_color("foo"); # 85ae99 |
| 34 |
|
| 35 |
say int2rgb(0xffffff); # ffffff |
| 36 |
|
| 37 |
say mix_2_rgb_colors('#ff0000', '#ffffff'); # pink (red + white) |
| 38 |
say mix_2_rgb_colors('ff0000', 'ffffff', 0.75); # pink with a whiter shade |
| 39 |
|
| 40 |
say mix_rgb_colors('ff0000', 1, 'ffffff', 1); # pink (red + white 1 : 1) |
| 41 |
say mix_rgb_colors('ff0000', 1, 'ffffff', 3); # pink with a whiter shade (red + white 1 : 3) |
| 42 |
say mix_rgb_colors('ff0000', 1, 'ffffff', 1, '0000ff', 0.5); # bluish pink |
| 43 |
|
| 44 |
say rand_rgb_color(); |
| 45 |
say rand_rgb_color('000000', '333333'); # limit range |
| 46 |
|
| 47 |
say rand_rgb_colors( |
| 48 |
{light_color => 1, avoid_colors=>[qw/ffffff ffcc00 ff00cc/], |
| 49 |
3); # ("e9f3d7", "e0bbcc", "63f88c") |
| 50 |
|
| 51 |
say reverse_rgb_color('0033CC'); # => ffcc33 |
| 52 |
|
| 53 |
say rgb2grayscale('0033CC'); # => 555555 # default 'average' algo |
| 54 |
say rgb2grayscale('0033CC', 'weighted_average'); # => 353535 |
| 55 |
|
| 56 |
say rgb2int("ffffff"); # 16777215 (which is 0xffffff) |
| 57 |
|
| 58 |
say rgb2sepia('0033CC'); # => 4d4535 |
| 59 |
|
| 60 |
say rgb_closest_to('cc211e'); # => "red" |
| 61 |
|
| 62 |
say rgb_distance('000000', '000000') # => 0 |
| 63 |
say rgb_distance('01f000', '04f400') # => 5 |
| 64 |
say rgb_distance('ffff00', 'ffffff') # => 255 |
| 65 |
|
| 66 |
say rgb_diff('000000', '000000'); # => 0 |
| 67 |
say rgb_diff('01f000', '04f400'); # => 5 |
| 68 |
say rgb_diff('ffff00', 'ffffff'); # => 255 |
| 69 |
say rgb_diff('000000', '000000', 'approx1'); # => 0 |
| 70 |
say rgb_diff('01f000', '04f400', 'approx1'); # => 9.06 |
| 71 |
say rgb_diff('ffff00', 'ffffff', 'approx1'); # => 360.98 |
| 72 |
|
| 73 |
say rgb_is_dark('404040'); # => 1 |
| 74 |
say rgb_is_dark('a0a0a0'); # => 0 |
| 75 |
say rgb_is_light('404040'); # => 0 |
| 76 |
say rgb_is_light('a0a0a0'); # => 1 |
| 77 |
|
| 78 |
say rgb_luminance('d090aa'); # => ffcc33 |
| 79 |
|
| 80 |
say tint_rgb_color('#ff8800', '#0033cc'); # => b36e3c |
| 81 |
|
| 82 |
DESCRIPTION |
| 83 |
FUNCTIONS |
| 84 |
None are exported by default, but they are exportable. |
| 85 |
|
| 86 |
assign_rgb_color |
| 87 |
Usage: |
| 88 |
|
| 89 |
my $rgb = assign_rgb_color($str); |
| 90 |
|
| 91 |
Map a string to an RGB color. This is done by producing SHA-1 digest |
| 92 |
(160bit, 20 bytes) of the string, then taking the first, 10th, and last |
| 93 |
byte to become the RGB color. |
| 94 |
|
| 95 |
See also: "assign_rgb_dark_color" and "assign_rgb_light_color". |
| 96 |
|
| 97 |
assign_rgb_dark_color |
| 98 |
Like "assign_rgb_color" except that it will make sure the assigned color |
| 99 |
is dark. |
| 100 |
|
| 101 |
assign_rgb_light_color |
| 102 |
Like "assign_rgb_color" except that it will make sure the assigned color |
| 103 |
is light. |
| 104 |
|
| 105 |
hsl2hsv |
| 106 |
Usage: |
| 107 |
|
| 108 |
my $hsl = hsl2hsv("0 1 0.5"); # => "0 1 1" |
| 109 |
|
| 110 |
Convert HSL to HSV. |
| 111 |
|
| 112 |
See also: "hsv2hsl". |
| 113 |
|
| 114 |
hsl2rgb |
| 115 |
Usage: |
| 116 |
|
| 117 |
my $rgb = hsl2rgb("0 1 0.5"); # => ff0000 |
| 118 |
|
| 119 |
Convert HSL to RGB. HSL should be given in a whitespace-separated H,S,L |
| 120 |
values e.g. "0 1 0.5". H (hue degree) has a range from 0-360 where 0 is |
| 121 |
red, 120 is green, 240 is blue and 360 is back to red. S (saturation) |
| 122 |
has a range from 0-1 where 0 is gray and 1 is fully saturated hue. L |
| 123 |
(lumination) has a range from 0-1 where 0 is fully black, 0.5 fully |
| 124 |
saturated, and 1 is fully white. |
| 125 |
|
| 126 |
See also "rgb2hsl". |
| 127 |
|
| 128 |
hsv2hsl |
| 129 |
Usage: |
| 130 |
|
| 131 |
my $hsl = hsv2hsl("0 1 1"); # => "0 1 0.5" |
| 132 |
|
| 133 |
Convert HSV to HSL. |
| 134 |
|
| 135 |
See also "hsl2hsv". |
| 136 |
|
| 137 |
hsv2rgb |
| 138 |
Usage: |
| 139 |
|
| 140 |
my $rgb = hsv2rgb("0 1 1"); # => ff0000 |
| 141 |
|
| 142 |
Convert HSV to RGB. HSV should be given in a whitespace-separated H,S,V |
| 143 |
values e.g. "0 1 1". H (hue degree) has a range from 0-360 where 0 is |
| 144 |
red, 120 is green, 240 is blue and 360 is back to red. S (saturation) |
| 145 |
has a range from 0-1 where 0 is gray and 1 is fully saturated hue. V |
| 146 |
(value) has a range from 0-1 where 0 is black and 1 is white. |
| 147 |
|
| 148 |
See also "rgb2hsv". |
| 149 |
|
| 150 |
int2rgb |
| 151 |
Usage: |
| 152 |
|
| 153 |
my $rgb = int2rgb(0xffffff); # => ffffff |
| 154 |
|
| 155 |
Convert integer to RGB string. |
| 156 |
|
| 157 |
See also "rgb2int". |
| 158 |
|
| 159 |
mix_2_rgb_colors |
| 160 |
Usage: |
| 161 |
|
| 162 |
my $mixed_rgb = mix_2_rgb_colors($rgb1, $rgb2, $pct); |
| 163 |
|
| 164 |
Mix 2 RGB colors. $pct is a number between 0 and 1, by default 0.5 |
| 165 |
(halfway), the closer to 1 the closer the resulting color to $rgb2. |
| 166 |
|
| 167 |
See also "mix_rgb_colors", "tint_rgb_color". |
| 168 |
|
| 169 |
mix_rgb_colors |
| 170 |
Usage: |
| 171 |
|
| 172 |
my $mixed_rgb = mix_rgb_colors($color1, $weight1, $color2, $weight2, ...); |
| 173 |
|
| 174 |
Mix several RGB colors. |
| 175 |
|
| 176 |
See also "mix_2_rgb_colors". |
| 177 |
|
| 178 |
rand_rgb_color |
| 179 |
Usage: |
| 180 |
|
| 181 |
my $rgb = rand_rgb_color([ $low_limit [ , $high_limit ] ]); |
| 182 |
|
| 183 |
Generate a random RGB color. You can specify the limit. Otherwise, they |
| 184 |
default to the full range (000000 to ffffff). |
| 185 |
|
| 186 |
See also "rand_rgb_colors". |
| 187 |
|
| 188 |
rand_rgb_colors |
| 189 |
Usage: |
| 190 |
|
| 191 |
my @rgbs = rand_rgb_colors([ \%opts ], $num=1); |
| 192 |
|
| 193 |
Produce $num random RGB colors, with some options. It does not (yet) |
| 194 |
create a palette of optimally distinct colors, but will make reasonable |
| 195 |
attempt to make the colors different from one another. |
| 196 |
|
| 197 |
Known options: |
| 198 |
|
| 199 |
* from_color |
| 200 |
|
| 201 |
RGB value, lower limit, optional. |
| 202 |
|
| 203 |
* to_color |
| 204 |
|
| 205 |
RGB value, high limit, optional. |
| 206 |
|
| 207 |
* light_color |
| 208 |
|
| 209 |
Boolean, default true. By default, this function will create light |
| 210 |
RGB colors, assuming the background color is dark, which is often |
| 211 |
the case in terminal. If this option is set to false, will create |
| 212 |
dark colors instead, If this option is set to undef, will create |
| 213 |
both dark and light colors. |
| 214 |
|
| 215 |
* avoid_colors |
| 216 |
|
| 217 |
Arrayref or hashref. List of colors to be avoided. You can put, for |
| 218 |
example, colors that you've already assigned/picked for your palette |
| 219 |
and don't want to use again. |
| 220 |
|
| 221 |
* max_attempts |
| 222 |
|
| 223 |
Uint, default 1000. Number of attempts to try generating the next |
| 224 |
random color if the generated color is rejected because it is |
| 225 |
light/dark, or because it's in "avoid_colors". |
| 226 |
|
| 227 |
When the number of attempts has been exceeded, the generated color |
| 228 |
is used anyway. |
| 229 |
|
| 230 |
* hash_prefix |
| 231 |
|
| 232 |
Whether to add hash prefix to produced color codes ("#123456") or |
| 233 |
not ("123456"). |
| 234 |
|
| 235 |
See also "rand_rgb_color". |
| 236 |
|
| 237 |
reverse_rgb_color |
| 238 |
Usage: |
| 239 |
|
| 240 |
my $reversed = reverse_rgb_color($rgb); |
| 241 |
|
| 242 |
Reverse $rgb. |
| 243 |
|
| 244 |
rgb2grayscale |
| 245 |
Usage: |
| 246 |
|
| 247 |
my $rgb_gs = rgb2grayscale($rgb [ , $algo ]); |
| 248 |
|
| 249 |
Convert $rgb to grayscale RGB value. There are several algorithms |
| 250 |
($algo) to choose from: |
| 251 |
|
| 252 |
* average |
| 253 |
|
| 254 |
The Average method takes the average value of R, G, and B as the |
| 255 |
grayscale value. |
| 256 |
|
| 257 |
Grayscale = (R + G + B ) / 3. |
| 258 |
|
| 259 |
The average method is simple but does not take into account the |
| 260 |
non-linearity of human vision (eyes are most sensitive to green, |
| 261 |
less to red, least to blue). |
| 262 |
|
| 263 |
* weighted_average |
| 264 |
|
| 265 |
This method gives weights to each of red, green, blue elements to |
| 266 |
take into account the sensitivity of human eyes. |
| 267 |
|
| 268 |
Grayscale = 0.299R + 0.587G + 0.114B |
| 269 |
|
| 270 |
See also rgb2sepia. |
| 271 |
|
| 272 |
rgb2hsl |
| 273 |
Usage: |
| 274 |
|
| 275 |
my $hsl = rgb2hsl($rgb); # example: "0 1 0.5" |
| 276 |
|
| 277 |
Convert RGB (0-255) to HSL. The result is a space-separated H, S, L |
| 278 |
values. |
| 279 |
|
| 280 |
See also "hsl2rgb". |
| 281 |
|
| 282 |
rgb2hsv |
| 283 |
Usage: |
| 284 |
|
| 285 |
my $hsv = rgb2hsv($rgb); # example: "0 1 255" |
| 286 |
|
| 287 |
Convert RGB (0-255) to HSV. The result is a space-separated H, S, V |
| 288 |
values. |
| 289 |
|
| 290 |
See also "hsv2rgb". |
| 291 |
|
| 292 |
rgb2int |
| 293 |
Usage: |
| 294 |
|
| 295 |
my $int = rgb2int("ffffff"); # => 16777216, which is 0xffffff |
| 296 |
|
| 297 |
Convert RGB string to integer. |
| 298 |
|
| 299 |
See also "int2rgb". |
| 300 |
|
| 301 |
rgb2sepia |
| 302 |
Usage: |
| 303 |
|
| 304 |
my $rgb_sepia = rgb2sepia($rgb); |
| 305 |
|
| 306 |
Convert $rgb to sepia tone RGB value. |
| 307 |
|
| 308 |
See also rgb2grayscale. |
| 309 |
|
| 310 |
rgb_closest_to |
| 311 |
Usage: |
| 312 |
|
| 313 |
my $colorname = rgb_closest_to( [ \%opts , ] $color); |
| 314 |
|
| 315 |
Return the primary color $color is closest to. Options: |
| 316 |
|
| 317 |
* colors |
| 318 |
|
| 319 |
A hash of color names and RGB values. If unspecified, the default |
| 320 |
is: |
| 321 |
|
| 322 |
{ |
| 323 |
black => "000000", |
| 324 |
blue => "0000ff", |
| 325 |
brown => "663333", |
| 326 |
cyan => "00ffff", # a.k.a. aqua |
| 327 |
green => "00ff00", |
| 328 |
grey => "808080", |
| 329 |
magenta => "ff00ff", # a.k.a. fuchsia |
| 330 |
orange => "ff8000", |
| 331 |
pink => "ffcccc", |
| 332 |
purple => "800080", # a.k.a. violet |
| 333 |
red => "ff0000", |
| 334 |
white => "ffffff", |
| 335 |
yellow => "ffff00", |
| 336 |
} |
| 337 |
|
| 338 |
rgb_diff |
| 339 |
Usage: |
| 340 |
|
| 341 |
my $dist = rgb_diff($rgb1, $rgb2[ , $algo ]) |
| 342 |
|
| 343 |
Calculate difference between two RGB colors, using one of several |
| 344 |
algorithms. |
| 345 |
|
| 346 |
* euclidean |
| 347 |
|
| 348 |
The default. It calculates the distance as: |
| 349 |
|
| 350 |
( (R1-R2)**2 + (G1-G2)**2 + (B1-B2)**2 )**0.5 |
| 351 |
|
| 352 |
which is the same as what "rgb_distance"() would produce. |
| 353 |
|
| 354 |
* approx1 |
| 355 |
|
| 356 |
This algorithm, described in [1] as "a low cost approximation" and |
| 357 |
"a combination both weighted Euclidean distance functions, where the |
| 358 |
weight factors depend on how big the 'red' component of the colour |
| 359 |
is" with "results that are very close to L*u*v" and "a more stable |
| 360 |
algorithm", uses the following formula: |
| 361 |
|
| 362 |
( 2*(R1-R2)**2 + 4*(G1-G2)**2 + 3*(B1-B2)**2 + Rm*((R1-R2)**2 - (B1-B2)**2)/256 )**0.5 |
| 363 |
|
| 364 |
where, Rm or "R mean" is (R1+R2)/2. |
| 365 |
|
| 366 |
* approx2 |
| 367 |
|
| 368 |
Like "approx1", but uses this formula: |
| 369 |
|
| 370 |
( 2*(R1-R2)**2 + 4*(G1-G2)**2 + 3*(B1-B2)**2 )**0.5 # if Rm < 128 |
| 371 |
( 3*(R1-R2)**2 + 4*(G1-G2)**2 + 2*(B1-B2)**2 )**0.5 # otherwise |
| 372 |
|
| 373 |
* hsv_euclidean |
| 374 |
|
| 375 |
Convert the RGB values to HSV, then calculate the HSV distance. |
| 376 |
Please see source code for details. |
| 377 |
|
| 378 |
* hsv_hue1 |
| 379 |
|
| 380 |
Like "hsv_euclidean" but puts more emphasis on hue difference. This |
| 381 |
algorithm is used, for example, by Color::ANSI::Util when mapping |
| 382 |
RGB 24bit color to the "closest" the ANSI 256-color or 16-color. |
| 383 |
This algorithm tends to choose the hued colors instead of favoring |
| 384 |
to fallback on white/gray, which is more preferred. |
| 385 |
|
| 386 |
TODO: redmean low-cost approximation, CMC l:c. |
| 387 |
|
| 388 |
For more about color difference, try reading |
| 389 |
<https://en.wikipedia.org/wiki/Color_difference>. |
| 390 |
|
| 391 |
[1] https://www.compuphase.com/cmetric.htm |
| 392 |
|
| 393 |
See also rgb_distance. |
| 394 |
|
| 395 |
rgb_distance |
| 396 |
Usage: |
| 397 |
|
| 398 |
my $dist = rgb_distance($rgb1, $rgb2) |
| 399 |
|
| 400 |
Calculate the euclidean RGB distance, using this formula: |
| 401 |
|
| 402 |
( (R1-R2)**2 + (G1-G2)**2 + (B1-B2)**2 )**0.5 |
| 403 |
|
| 404 |
For example, the distance between "000000" and "ffffff" is ~441.67, |
| 405 |
while the distance between "ffff00" and "ffffff" is 255. |
| 406 |
|
| 407 |
See also rgb_diff. |
| 408 |
|
| 409 |
rgb_is_dark |
| 410 |
Usage: |
| 411 |
|
| 412 |
my $is_dark = rgb_is_dark($rgb) |
| 413 |
|
| 414 |
Return true if $rgb is a "dark" color, which is determined by checking |
| 415 |
if the RGB distance to "000000" is smaller than to "ffffff". |
| 416 |
|
| 417 |
See also "rgb_is_light". |
| 418 |
|
| 419 |
rgb_is_light |
| 420 |
Usage: |
| 421 |
|
| 422 |
my $is_light = rgb_is_light($rgb) |
| 423 |
|
| 424 |
Return true if $rgb is a "light" color, which is determined by checking |
| 425 |
if the RGB distance to "000000" is larger than to "ffffff". |
| 426 |
|
| 427 |
See also "rgb_is_dark". |
| 428 |
|
| 429 |
rgb_luminance |
| 430 |
Usage: |
| 431 |
|
| 432 |
my $luminance = rgb_luminance($rgb); |
| 433 |
|
| 434 |
Calculate standard/objective luminance from RGB value using this |
| 435 |
formula: |
| 436 |
|
| 437 |
(0.2126*R) + (0.7152*G) + (0.0722*B) |
| 438 |
|
| 439 |
where R, G, and B range from 0 to 1. Return a number from 0 to 1. |
| 440 |
|
| 441 |
tint_rgb_color |
| 442 |
Usage: |
| 443 |
|
| 444 |
my $new_rgb = tint_rgb_color($rgb, $tint_rgb, $pct) |
| 445 |
|
| 446 |
Tint $rgb with $tint_rgb. $pct is by default 0.5. It is similar to |
| 447 |
mixing, but the less luminance the color is the less it is tinted with |
| 448 |
the tint color. This has the effect of black color still being black |
| 449 |
instead of becoming tinted. |
| 450 |
|
| 451 |
See also mix_2_rgb_colors, mix_rgb_colors. |
| 452 |
|
| 453 |
HOMEPAGE |
| 454 |
Please visit the project's homepage at |
| 455 |
<https://metacpan.org/release/Color-RGB-Util>. |
| 456 |
|
| 457 |
SOURCE |
| 458 |
Source repository is at |
| 459 |
<https://github.com/perlancar/perl-Color-RGB-Util>. |
| 460 |
|
| 461 |
SEE ALSO |
| 462 |
Color::ANSI::Util |
| 463 |
|
| 464 |
AUTHOR |
| 465 |
perlancar <[email protected]> |
| 466 |
|
| 467 |
CONTRIBUTOR |
| 468 |
Steven Haryanto <[email protected]> |
| 469 |
|
| 470 |
CONTRIBUTING |
| 471 |
To contribute, you can send patches by email/via RT, or send pull |
| 472 |
requests on GitHub. |
| 473 |
|
| 474 |
Most of the time, you don't need to build the distribution yourself. You |
| 475 |
can simply modify the code, then test via: |
| 476 |
|
| 477 |
% prove -l |
| 478 |
|
| 479 |
If you want to build the distribution (e.g. to try to install it locally |
| 480 |
on your system), you can install Dist::Zilla, |
| 481 |
Dist::Zilla::PluginBundle::Author::PERLANCAR, |
| 482 |
Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two |
| 483 |
other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps |
| 484 |
required beyond that are considered a bug and can be reported to me. |
| 485 |
|
| 486 |
COPYRIGHT AND LICENSE |
| 487 |
This software is copyright (c) 2025 by perlancar <[email protected]>. |
| 488 |
|
| 489 |
This is free software; you can redistribute it and/or modify it under |
| 490 |
the same terms as the Perl 5 programming language system itself. |
| 491 |
|
| 492 |
BUGS |
| 493 |
Please report any bugs or feature requests on the bugtracker website |
| 494 |
<https://rt.cpan.org/Public/Dist/Display.html?Name=Color-RGB-Util> |
| 495 |
|
| 496 |
When submitting a bug or request, please include a test-file or a patch |
| 497 |
to an existing test-file that illustrates the bug or desired feature. |
| 498 |
|