#!/usr/bin/env perl # $XTermId: closest-rgb.pl,v 1.12 2020/12/13 15:07:02 tom Exp $ # ----------------------------------------------------------------------------- # this file is part of xterm # # Copyright 2017-2018,2020 by Thomas E. Dickey # # All Rights Reserved # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # # Except as contained in this notice, the name(s) of the above copyright # holders shall not be used in advertising or otherwise to promote the # sale, use or other dealings in this Software without prior written # authorization. # ----------------------------------------------------------------------------- # For a given RGB value, show its distance from xterm's 88/256-color # models or alternatively against rgb.txt use strict; use warnings; use Getopt::Std; our $namedRGB = "/etc/X11/rgb.txt"; our @namedRGB; our @xtermRGB; our ( $opt_f, $opt_i, $opt_n ); sub main::HELP_MESSAGE() { printf STDERR <; close $fp; my @result; my $o = 0; for my $i ( 0 .. $#data ) { next if ( $data[$i] =~ /^\s*[[:punct:]]/ ); $result[ $o++ ] = &lookup( $data[$i] ); } return @result; } sub distance($$) { my %a = %{ $_[0] }; my %b = %{ $_[1] }; my $R = $a{R} - $b{R}; my $G = $a{G} - $b{G}; my $B = $a{B} - $b{B}; my $result = sqrt( $R * $R + $G * $G + $B * $B ); } sub show_distances($$) { my @ref = @{ $_[0] }; my @cmp = @{ $_[1] }; for my $c ( 0 .. $#cmp ) { my %cmp = %{ $cmp[$c] }; my $best = -1; my %best; for my $r ( 0 .. $#ref ) { my %ref = %{ $ref[$r] }; my $test = &distance( \%ref, \%cmp ); if ( $best < 0 ) { $best = $test; %best = %ref; } elsif ( $best > $test ) { $best = $test; %best = %ref; } } printf "%3d %-25s %5.1f %s\n", $c, $cmp{NAME}, $best, $best{NAME}; } } @namedRGB = &load_namedRGB($opt_f); printf "%d names from $opt_f\n", $#namedRGB + 1; if ( $opt_n <= 16 ) { @xtermRGB = &xterm16; } elsif ( $opt_n <= 88 ) { @xtermRGB = &xterm88; } else { @xtermRGB = &xterm256; } printf "%d names from xterm palette\n", $#xtermRGB + 1; &show_distances( \@xtermRGB, \@namedRGB ) if ($opt_i); &show_distances( \@namedRGB, \@xtermRGB ) unless ($opt_i); 1;