[Bio] / FigKernelPackages / gjocolorlib.pm Repository:
ViewVC logotype

View of /FigKernelPackages/gjocolorlib.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (download) (as text) (annotate)
Fri Sep 17 23:59:35 2010 UTC (9 years, 2 months ago) by overbeek
Branch: MAIN
CVS Tags: rast_rel_2010_0928, rast_rel_2010_1206
Changes since 1.5: +2 -2 lines
fixes to gray

package gjocolorlib;

use strict;
eval { use Data::Dumper };

#  Invoke with:
#
#      use gjocolorlib;
#
#  Based on component values from 0 to 1:
#
#     $rgb  = [ $red, $green, $blue ]
#     $hsb  = [ $hue, $saturation, $brightness ]
#     $cmy  = [ $cyan, $magenta, $yellow ]
#     $cmyk = [ $cyan, $magenta, $yellow, $black ]

#  Exported functions:
#
#     $rgb  = hsb2rgb( $hsb )
#     @rgb  = hsb2rgb( @hsb )
#     $rgb  = cmy2rgb( $cmy )
#     @rgb  = cmy2rgb( @cmy )
#     $rgb  = cmyk2rgb( $cmyk )
#     @rgb  = cmyk2rgb( @cmyk )
#     $rgb  = html2rgb( $html )
#     @rgb  = html2rgb( $html )
#     $gray = rgb2gray( $rgb )
#     $gray = rgb2gray( @rgb )
#     $html = rgb2html( $rgb )
#     $html = rgb2html( @rgb )
#     $html = gray2html( $gray )
#     $name = rgb2name( $rgb )
#     $name = rgb2name( @rgb )
#
#     $rgb  = blend_rgb_colors( $color1, $color2, ... )
#     @rgb  = blend_rgb_colors( $color1, $color2, ... )
#     $html = blend_html_colors( $color1, $color2, ... )


require Exporter;

our @ISA = qw(Exporter);
our @EXPORT = qw(
        hsb2rgb
        cmy2rgb
        cmyk2rgb
        html2rgb
        rgb2gray
        rgb2html
        gray2html
        rgb2name
        blend_rgb_colors
        blend_html_colors
        );
our @EXPORT_OK = qw(
        UI_Orange
        UI_Formal_Orange
        UI_Blue
        UI_Formal_Blue
        );


my $UI_Orange        = [       1, 102/255,       0 ];
my $UI_Formal_Orange = [ 204/255, 102/255,       0 ];
my $UI_Blue          = [       0,  51/255, 102/255 ];
my $UI_Formal_Blue   = [  51/255,  51/255, 102/255 ];

sub floor {
    my $x = $_[0];
    defined( $x ) || return undef;
    ( $x >= 0 ) || ( int($x) == $x ) ? int( $x ) : -1 - int( - $x )
}
sub min { my ( $a, $b ) = @_; ( $a < $b ) ? $a : $b }
sub max { my ( $a, $b ) = @_; ( $a > $b ) ? $a : $b }


#  Convert HSB to RGB:
#  Here, hue is taken to be in range 0 - 1;
#  It is sometimes 0 - 6 and sometimes 0 - 2*pi

sub hsb2rgb {
    my ( $h, $s, $br ) = @_;
    if ( ref( $h ) eq "ARRAY" ) { ( $h, $s, $br ) = @$h }
    defined( $h ) && defined( $s ) && defined( $br ) || return undef;

    $h = 6 * ($h - floor($h));      # Hue is made cyclic modulo 1
    if ( $s  > 1 ) { $s  = 1 } elsif ( $s  < 0 ) { $s  = 0 }  # 0 <= s  <= 1
    if ( $br > 1 ) { $br = 1 } elsif ( $br < 0 ) { $br = 0 }  # 0 <= br <= 1
    my ( $r, $g, $b ) = ( $h <= 3 ) ? ( ( $h <= 1 ) ? ( 1,      $h,     0      )
                                      : ( $h <= 2 ) ? ( 2 - $h, 1,      0      )
                                      :               ( 0,      1,      $h - 2 )
                                      )
                                    : ( ( $h <= 4 ) ? ( 0,      4 - $h, 1      )
                                      : ( $h <= 5 ) ? ( $h - 4, 0,      1      )
                                      :               ( 1,      0,      6 - $h )
                                      );
    my @rgb = ( ( $r * $s + 1 - $s ) * $br,
                ( $g * $s + 1 - $s ) * $br,
                ( $b * $s + 1 - $s ) * $br
              );
    wantarray() ? @rgb : \@rgb
}


#  Convert CMY to RGB:

sub cmy2rgb {
    my ( $c, $m, $y ) = @_;
    if ( ref( $c ) eq "ARRAY" ) { ( $c, $m, $y ) = @$c }
    defined( $c ) && defined( $m ) && defined( $y ) || return undef;
    if ( $c > 1 ) { $c = 1 } elsif ( $c < 0 ) { $c = 0 }
    if ( $m > 1 ) { $m = 1 } elsif ( $m < 0 ) { $m = 0 }
    if ( $y > 1 ) { $y = 1 } elsif ( $y < 0 ) { $y = 0 }
    wantarray() ? ( 1 - $c, 1 - $m, 1 - $y ) : [ 1 - $c, 1 - $m, 1 - $y ]
}


#  Convert CMYK to RGB:

sub cmyk2rgb {
    my ( $c, $m, $y, $k ) = @_;
    if ( ref( $c ) eq "ARRAY" ) { ( $c, $m, $y, $k ) = @$c }
    defined( $c ) && defined( $m ) && defined( $y ) && defined( $k ) || return undef;
    if ( $c > 1 ) { $c = 1 } elsif ( $c < 0 ) { $c = 0 }
    if ( $m > 1 ) { $m = 1 } elsif ( $m < 0 ) { $m = 0 }
    if ( $y > 1 ) { $y = 1 } elsif ( $y < 0 ) { $y = 0 }
    my $br = ($k < 0) ? 1 : ($k > 1) ? 0 : 1 - $k;
    my @rgb = ( ( 1 - $c ) * $br, ( 1 - $m ) * $br, ( 1 - $y ) * $br );
    wantarray() ? @rgb : \@rgb
}

#  Named colors in HTML specification.  Note that the extra color
#  names included from X11 are not linked in a logical progression.
#  This is probably due to a different assumed gamma.

my %name2html = map { lc }
      ( Black => '#000000',
        Blue => '#0000FF',
        CadetBlue3 => '#77BFC7',
        CadetBlue4 => '#4C787E',
        Chartreuse => '#8AFB17',
        Chartreuse2 => '#7FE817',
        Chartreuse3 => '#6CC417',
        Chartreuse4 => '#437C17',
        Chocolate => '#C85A17',
        Coral => '#F76541',
        Coral2 => '#E55B3C',
        Coral3 => '#C34A2C',
        CornflowerBlue => '#151B8D',
        Cyan => '#00FFFF',
        Cyan1 => '#57FEFF',
        Cyan2 => '#50EBEC',
        Cyan3 => '#46C7C7',
        Cyan4 => '#307D7E',
        DarkGoldenrod => '#AF7817',
        DarkGoldenrod1 => '#FBB117',
        DarkGoldenrod2 => '#E8A317',
        DarkGoldenrod3 => '#C58917',
        DarkGoldenrod4 => '#7F5217',
        DarkGreen => '#254117',
        DarkOliveGreen1 => '#CCFB5D',
        DarkOliveGreen2 => '#BCE954',
        DarkOliveGreen3 => '#A0C544',
        DarkOliveGreen4 => '#667C26',
        DarkOrange => '#F88017',
        DarkOrange1 => '#F87217',
        DarkOrange2 => '#E56717',
        DarkOrange3 => '#7E3117',
        DarkOrange3 => '#C35617',
        DarkOrchid => '#7D1B7E',
        DarkOrchid1 => '#B041FF',
        DarkOrchid2 => '#A23BEC',
        DarkOrchid3 => '#8B31C7',
        DarkOrchid4 => '#571B7e',
        DarkSalmon => '#E18B6B',
        DarkSeaGreen => '#8BB381',
        DarkSeaGreen1 => '#C3FDB8',
        DarkSeaGreen2 => '#B5EAAA',
        DarkSeaGreen3 => '#99C68E',
        DarkSeaGreen4 => '#617C58',
        DarkSlateBlue => '#2B3856',
        DarkSlateGray => '#25383C',
        DarkSlateGray1 => '#9AFEFF',
        DarkSlateGray2 => '#8EEBEC',
        DarkSlateGray3 => '#78c7c7',
        DarkSlateGray4 => '#4C7D7E',
        DarkTurquoise => '#3B9C9C',
        DarkViolet => '#842DCE',
        DeepPink => '#F52887',
        DeepPink2 => '#E4287C',
        DeepPink3 => '#C12267',
        DeepPink4 => '#7D053F',
        DeepSkyBlue => '#3BB9FF',
        DeepSkyBlue2 => '#38ACEC',
        DeepSkyBlue3 => '#3090C7',
        DeepSkyBlue4 => '#25587E',
        DimGray => '#463E41',
        DodgerBlue => '#1589FF',
        DodgerBlue2 => '#157DEC',
        DodgerBlue3 => '#1569C7',
        DodgerBlue4 => '#153E7E',
        Firebrick => '#800517',
        Firebrick1 => '#F62817',
        Firebrick2 => '#E42217',
        Firebrick3 => '#C11B17',
        ForestGreen => '#4E9258',
        Gold => '#D4A017',
        Gold1 => '#FDD017',
        Gold2 => '#EAC117',
        Gold3 => '#C7A317',
        Gold4 => '#806517',
        Goldenrod => '#EDDA74',
        Goldenrod1 => '#FBB917',
        Goldenrod2 => '#E9AB17',
        Goldenrod3 => '#C68E17',
        Goldenrod4 => '#805817',
        Gray => '#736F6E',
        Gray0 => '#150517',
        Gray18 => '#250517',
        Gray21 => '#2B1B17',
        Gray23 => '#302217',
        Gray24 => '#302226',
        Gray25 => '#342826',
        Gray26 => '#34282C',
        Gray27 => '#382D2C',
        Gray28 => '#3b3131',
        Gray29 => '#3E3535',
        Gray30 => '#413839',
        Gray31 => '#41383C',
        Gray32 => '#463E3F',
        Gray34 => '#4A4344',
        Gray35 => '#4C4646',
        Gray36 => '#4E4848',
        Gray37 => '#504A4B',
        Gray38 => '#544E4F',
        Gray39 => '#565051',
        Gray40 => '#595454',
        Gray41 => '#5C5858',
        Gray42 => '#5F5A59',
        Gray43 => '#625D5D',
        Gray44 => '#646060',
        Gray45 => '#666362',
        Gray46 => '#696565',
        Gray47 => '#6D6968',
        Gray48 => '#6E6A6B',
        Gray49 => '#726E6D',
        Gray50 => '#747170',
        Green => '#00FF00',
        Green1 => '#5FFB17',
        Green2 => '#59E817',
        Green3 => '#4CC417',
        Green4 => '#347C17',
        GreenYellow => '#B1FB17',
        HotPink => '#F660AB',
        HotPink1 => '#F665AB',
        HotPink2 => '#E45E9D',
        HotPink3 => '#C25283',
        HotPink4 => '#7D2252',
        IndianRed1 => '#F75D59',
        IndianRed2 => '#E55451',
        IndianRed3 => '#C24641',
        IndianRed4 => '#7E2217',
        Khaki => '#ADA96E',
        Khaki1 => '#FFF380',
        Khaki2 => '#EDE275',
        Khaki3 => '#C9BE62',
        Khaki4 => '#827839',
        Lavendar => '#E3E4FA',
        LavendarBlush => '#FDEEF4',
        LavendarBlush2 => '#EBDDE2',
        LavendarBlush3 => '#C8BBBE',
        LavendarBlush4 => '#817679',
        LawnGreen => '#87F717',
        LemonChiffon => '#FFF8C6',
        LemonChiffon2 => '#ECE5B6',
        LemonChiffon3 => '#C9C299',
        LemonChiffon4 => '#827B60',
        LightBlue => '#ADDFFF',
        LightBlue1 => '#BDEDFF',
        LightBlue2 => '#AFDCEC',
        LightBlue3 => '#95B9C7',
        LightBlue4 => '#5E767E',
        LightCoral => '#E77471',
        LightCyan => '#E0FFFF',
        LightCyan2 => '#CFECEC',
        LightCyan3 => '#AFC7C7',
        LightCyan4 => '#717D7D',
        LightGolden2 => '#ECD672',
        LightGoldenrod => '#ECD872',
        LightGoldenrod1 => '#FFE87C',
        LightGoldenrod3 => '#C8B560',
        LightGoldenrod4 => '#817339',
        LightGoldenrodYellow => '#FAF8CC',
        LightPink => '#FAAFBA',
        LightPink1 => '#F9A7B0',
        LightPink2 => '#E799A3',
        LightPink3 => '#C48189',
        LightPink4 => '#7F4E52',
        LightSalmon => '#F9966B',
        LightSalmon2 => '#E78A61',
        LightSalmon3 => '#C47451',
        LightSalmon4 => '#7F462C',
        LightSeaGreen => '#3EA99F',
        LightSkyBlue => '#82CAFA',
        LightSkyBlue2 => '#A0CFEC',
        LightSkyBlue3 => '#87AFC7',
        LightSkyBlue4 => '#566D7E',
        LightSlateBlue => '#736AFF',
        LightSlateGray => '#6D7B8D',
        LightSteelBlue => '#728FCE',
        LightSteelBlue1 => '#C6DEFF',
        LightSteelBlue2 => '#B7CEEC',
        LightSteelBlue4 => '#646D7E',
        LimeGreen => '#41A317',
        Magenta => '#FF00FF',
        Magenta1 => '#F433FF',
        Magenta2 => '#E238EC',
        Magenta3 => '#C031C7',
        Maroon => '#810541',
        Maroon1 => '#F535AA',
        Maroon2 => '#E3319D',
        Maroon3 => '#C12283',
        Maroon4 => '#7D0552',
        MediumAquamarine => '#348781',
        MediumForestGreen => '#347235',
        MediumOrchid => '#B048B5',
        MediumOrchid1 => '#D462FF',
        MediumOrchid2 => '#C45AEC',
        MediumOrchid3 => '#A74AC7',
        MediumOrchid4 => '#6A287E',
        MediumPurple => '#8467D7',
        MediumPurple1 => '#9E7BFF',
        MediumPurple2 => '#9172EC',
        MediumPurple3 => '#7A5DC7',
        MediumPurple4 => '#4E387E',
        MediumSeaGreen => '#306754',
        MediumSlateBlue => '#5E5A80',
        MediumSpringGreen => '#348017',
        MediumTurquoise => '#48CCCD',
        MediumVioletRed => '#CA226B',
        MidnightBlue => '#151B54',
        PaleTurquoise3 => '#92C7C7',
        PaleTurquoise4 => '#5E7D7E',
        PaleVioletRed => '#D16587',
        PaleVioletRed1 => '#F778A1',
        PaleVioletRed2 => '#E56E94',
        PaleVioletRed3 => '#C25A7C',
        PaleVioletRed4 => '#7E354D',
        Pink => '#FAAFBE',
        Pink2 => '#E7A1B0',
        Pink3 => '#C48793',
        Pink4 => '#7F525D',
        Plum => '#B93B8F',
        Plum1 => '#F9B7FF',
        Plum2 => '#E6A9EC',
        Plum3 => '#C38EC7',
        Plum4 => '#7E587E',
        Purple => '#8E35EF',
        Purple1 => '#893BFF',
        Purple2 => '#7F38EC',
        Purple3 => '#6C2DC7',
        Purple4 => '#461B7E',
        Red => '#FF0000',
        Red1 => '#F62217',
        Red2 => '#E41B17',
        RosyBrown => '#B38481',
        RosyBrown1 => '#FBBBB9',
        RosyBrown2 => '#E8ADAA',
        RosyBrown3 => '#C5908E',
        RosyBrown4 => '#7F5A58',
        RoyalBlue => '#2B60DE',
        RoyalBlue1 => '#306EFF',
        RoyalBlue2 => '#2B65EC',
        RoyalBlue3 => '#2554C7',
        RoyalBlue4 => '#15317E',
        Salmon1 => '#F88158',
        Salmon2 => '#E67451',
        Salmon3 => '#C36241',
        Salmon4 => '#7E3817',
        SandyBrown => '#EE9A4D',
        SeaGreen => '#4E8975',
        SeaGreen1 => '#6AFB92',
        SeaGreen2 => '#64E986',
        SeaGreen3 => '#54C571',
        SeaGreen4 => '#387C44',
        Sienna => '#8A4117',
        Sienna1 => '#F87431',
        Sienna2 => '#E66C2C',
        Sienna3 => '#C35817',
        Sienna4 => '#7E3517',
        SkyBlue => '#6698FF',
        SkyBlue1 => '#82CAFF',
        SkyBlue2 => '#79BAEC',
        SkyBlue3 => '#659EC7',
        SkyBlue4 => '#41627E',
        SlateBlue => '#3574EC7',
        SlateBlue => '#737CA1',
        SlateBlue => '#737CA1',
        SlateBlue2 => '#6960EC',
        SlateBlue4 => '#342D7E',
        SlateGray => '#657383',
        SlateGray1 => '#C2DFFF',
        SlateGray2 => '#B4CFEC',
        SlateGray3 => '#98AFC7',
        SlateGray4 => '#616D7E',
        SpringGreen => '#4AA02C',
        SpringGreen => '#4AA02C',
        SpringGreen1 => '#5EFB6E',
        SpringGreen2 => '#57E964',
        SpringGreen3 => '#4CC552',
        SpringGreen4 => '#347C2C',
        SteelBlue => '#4863A0',
        SteelBlue1 => '#5CB3FF',
        SteelBlue2 => '#56A5EC',
        SteelBlue3 => '#488AC7',
        SteelBlue4 => '#2B547E',
        Thistle => '#D2B9D3',
        Thistle1 => '#FCDFFF',
        Thistle2 => '#E9CFEC',
        Thistle3 => '#C6AEC7',
        Thistle4 => '#806D7E',
        Turquoise => '#43C6DB',
        Turquoise1 => '#52F3FF',
        Turquoise2 => '#4EE2EC',
        Turquoise3 => '#43BFC7',
        White => '#FFFFFF',
        Violet => '#8D38C9',
        VioletRed => '#F6358A',
        VioletRed1 => '#F6358A',
        VioletRed2 => '#E4317F',
        VioletRed3 => '#C12869',
        VioletRed4 => '#7D0541',
        Yellow => '#FFFF00',
        Yellow1 => '#FFFC17',
        YellowGreen => '#52D017',
        #  Additional color names from X11
        aliceblue => '#f0f8ff',
        antiquewhite => '#faebd7',
        antiquewhite1 => '#ffefdb',
        antiquewhite2 => '#eedfcc',
        antiquewhite3 => '#cdc0b0',
        antiquewhite4 => '#8b8378',
        aquamarine => '#7fffd4',
        aquamarine1 => '#7fffd4',
        aquamarine2 => '#76eec6',
        aquamarine3 => '#66cdaa',
        aquamarine4 => '#458b74',
        azure => '#f0ffff',
        azure1 => '#f0ffff',
        azure2 => '#e0eeee',
        azure3 => '#c1cdcd',
        azure4 => '#838b8b',
        beige => '#f5f5dc',
        bisque => '#ffe4c4',
        bisque1 => '#ffe4c4',
        bisque2 => '#eed5b7',
        bisque3 => '#cdb79e',
        bisque4 => '#8b7d6b',
        blanchedalmond => '#ffebcd',
        blue1 => '#0000ff',
        blue2 => '#0000ee',
        blue3 => '#0000cd',
        blue4 => '#00008b',
        blueviolet => '#8a2be2',
        brown => '#a52a2a',
        brown1 => '#ff4040',
        brown2 => '#ee3b3b',
        brown3 => '#cd3333',
        brown4 => '#8b2323',
        burlywood => '#deb887',
        burlywood1 => '#ffd39b',
        burlywood2 => '#eec591',
        burlywood3 => '#cdaa7d',
        burlywood4 => '#8b7355',
        cadetblue => '#5f9ea0',
        cadetblue1 => '#98f5ff',
        cadetblue2 => '#8ee5ee',
        chartreuse1 => '#7fff00',
        chocolate1 => '#ff7f24',
        chocolate2 => '#ee7621',
        chocolate3 => '#cd661d',
        chocolate4 => '#8b4513',
        coral1 => '#ff7256',
        coral4 => '#8b3e2f',
        cornsilk => '#fff8dc',
        cornsilk1 => '#fff8dc',
        cornsilk2 => '#eee8cd',
        cornsilk3 => '#cdc8b1',
        cornsilk4 => '#8b8878',
        darkblue => '#00008b',
        darkcyan => '#008b8b',
        darkgray => '#a9a9a9',
        darkgrey => '#a9a9a9',
        darkkhaki => '#bdb76b',
        darkmagenta => '#8b008b',
        darkolivegreen => '#556b2f',
        darkorange4 => '#8b4500',
        darkred => '#8b0000',
        darkslategrey => '#2f4f4f',
        darkslategrey1 => '#97ffff',
        darkslategrey2 => '#8deeee',
        darkslategrey3 => '#79cdcd',
        darkslategrey4 => '#528b8b',
        deeppink1 => '#ff1493',
        deepskyblue1 => '#00bfff',
        dimgrey => '#696969',
        dodgerblue1 => '#1e90ff',
        firebrick4 => '#8b1a1a',
        floralwhite => '#fffaf0',
        gainsboro => '#dcdcdc',
        ghostwhite => '#f8f8ff',
        gray1 => '#030303',
        gray10 => '#1a1a1a',
        gray100 => '#ffffff',
        gray11 => '#1c1c1c',
        gray12 => '#1f1f1f',
        gray13 => '#212121',
        gray14 => '#242424',
        gray15 => '#262626',
        gray16 => '#292929',
        gray17 => '#2b2b2b',
        gray19 => '#303030',
        gray2 => '#050505',
        gray20 => '#333333',
        gray22 => '#383838',
        gray3 => '#080808',
        gray33 => '#545454',
        gray4 => '#0a0a0a',
        gray5 => '#0d0d0d',
        gray51 => '#828282',
        gray52 => '#858585',
        gray53 => '#878787',
        gray54 => '#8a8a8a',
        gray55 => '#8c8c8c',
        gray56 => '#8f8f8f',
        gray57 => '#919191',
        gray58 => '#949494',
        gray59 => '#969696',
        gray6 => '#0f0f0f',
        gray60 => '#999999',
        gray61 => '#9c9c9c',
        gray62 => '#9e9e9e',
        gray63 => '#a1a1a1',
        gray64 => '#a3a3a3',
        gray65 => '#a6a6a6',
        gray66 => '#a8a8a8',
        gray67 => '#ababab',
        gray68 => '#adadad',
        gray69 => '#b0b0b0',
        gray7 => '#121212',
        gray70 => '#b3b3b3',
        gray71 => '#b5b5b5',
        gray72 => '#b8b8b8',
        gray73 => '#bababa',
        gray74 => '#bdbdbd',
        gray75 => '#bfbfbf',
        gray76 => '#c2c2c2',
        gray77 => '#c4c4c4',
        gray78 => '#c7c7c7',
        gray79 => '#c9c9c9',
        gray8 => '#141414',
        gray80 => '#cccccc',
        gray81 => '#cfcfcf',
        gray82 => '#d1d1d1',
        gray83 => '#d4d4d4',
        gray84 => '#d6d6d6',
        gray85 => '#d9d9d9',
        gray86 => '#dbdbdb',
        gray87 => '#dedede',
        gray88 => '#e0e0e0',
        gray89 => '#e3e3e3',
        gray9 => '#171717',
        gray90 => '#e5e5e5',
        gray91 => '#e8e8e8',
        gray92 => '#ebebeb',
        gray93 => '#ededed',
        gray94 => '#f0f0f0',
        gray95 => '#f2f2f2',
        gray96 => '#f5f5f5',
        gray97 => '#f7f7f7',
        gray98 => '#fafafa',
        gray99 => '#fcfcfc',
        grey => '#bebebe',
        grey0 => '#000000',
        grey1 => '#030303',
        grey10 => '#1a1a1a',
        grey100 => '#ffffff',
        grey11 => '#1c1c1c',
        grey12 => '#1f1f1f',
        grey13 => '#212121',
        grey14 => '#242424',
        grey15 => '#262626',
        grey16 => '#292929',
        grey17 => '#2b2b2b',
        grey18 => '#2e2e2e',
        grey19 => '#303030',
        grey2 => '#050505',
        grey20 => '#333333',
        grey21 => '#363636',
        grey22 => '#383838',
        grey23 => '#3b3b3b',
        grey24 => '#3d3d3d',
        grey25 => '#404040',
        grey26 => '#424242',
        grey27 => '#454545',
        grey28 => '#474747',
        grey29 => '#4a4a4a',
        grey3 => '#080808',
        grey30 => '#4d4d4d',
        grey31 => '#4f4f4f',
        grey32 => '#525252',
        grey33 => '#545454',
        grey34 => '#575757',
        grey35 => '#595959',
        grey36 => '#5c5c5c',
        grey37 => '#5e5e5e',
        grey38 => '#616161',
        grey39 => '#636363',
        grey4 => '#0a0a0a',
        grey40 => '#666666',
        grey41 => '#696969',
        grey42 => '#6b6b6b',
        grey43 => '#6e6e6e',
        grey44 => '#707070',
        grey45 => '#737373',
        grey46 => '#757575',
        grey47 => '#787878',
        grey48 => '#7a7a7a',
        grey49 => '#7d7d7d',
        grey5 => '#0d0d0d',
        grey50 => '#7f7f7f',
        grey51 => '#828282',
        grey52 => '#858585',
        grey53 => '#878787',
        grey54 => '#8a8a8a',
        grey55 => '#8c8c8c',
        grey56 => '#8f8f8f',
        grey57 => '#919191',
        grey58 => '#949494',
        grey59 => '#969696',
        grey6 => '#0f0f0f',
        grey60 => '#999999',
        grey61 => '#9c9c9c',
        grey62 => '#9e9e9e',
        grey63 => '#a1a1a1',
        grey64 => '#a3a3a3',
        grey65 => '#a6a6a6',
        grey66 => '#a8a8a8',
        grey67 => '#ababab',
        grey68 => '#adadad',
        grey69 => '#b0b0b0',
        grey7 => '#121212',
        grey70 => '#b3b3b3',
        grey71 => '#b5b5b5',
        grey72 => '#b8b8b8',
        grey73 => '#bababa',
        grey74 => '#bdbdbd',
        grey75 => '#bfbfbf',
        grey76 => '#c2c2c2',
        grey77 => '#c4c4c4',
        grey78 => '#c7c7c7',
        grey79 => '#c9c9c9',
        grey8 => '#141414',
        grey80 => '#cccccc',
        grey81 => '#cfcfcf',
        grey82 => '#d1d1d1',
        grey83 => '#d4d4d4',
        grey84 => '#d6d6d6',
        grey85 => '#d9d9d9',
        grey86 => '#dbdbdb',
        grey87 => '#dedede',
        grey88 => '#e0e0e0',
        grey89 => '#e3e3e3',
        grey9 => '#171717',
        grey90 => '#e5e5e5',
        grey91 => '#e8e8e8',
        grey92 => '#ebebeb',
        grey93 => '#ededed',
        grey94 => '#f0f0f0',
        grey95 => '#f2f2f2',
        grey96 => '#f5f5f5',
        grey97 => '#f7f7f7',
        grey98 => '#fafafa',
        grey99 => '#fcfcfc',
        honeydew => '#f0fff0',
        honeydew1 => '#f0fff0',
        honeydew2 => '#e0eee0',
        honeydew3 => '#c1cdc1',
        honeydew4 => '#838b83',
        indianred => '#cd5c5c',
        ivory => '#fffff0',
        ivory1 => '#fffff0',
        ivory2 => '#eeeee0',
        ivory3 => '#cdcdc1',
        ivory4 => '#8b8b83',
        lavender => '#e6e6fa',
        lavenderblush => '#fff0f5',
        lavenderblush1 => '#fff0f5',
        lavenderblush2 => '#eee0e5',
        lavenderblush3 => '#cdc1c5',
        lavenderblush4 => '#8b8386',
        lemonchiffon1 => '#fffacd',
        lightcyan1 => '#e0ffff',
        lightgoldenrod2 => '#eedc82',
        lightgray => '#d3d3d3',
        lightgreen => '#90ee90',
        lightgrey => '#d3d3d3',
        lightsalmon1 => '#ffa07a',
        lightskyblue1 => '#b0e2ff',
        lightslategrey => '#778899',
        lightsteelblue3 => '#a2b5cd',
        lightyellow => '#ffffe0',
        lightyellow1 => '#ffffe0',
        lightyellow2 => '#eeeed1',
        lightyellow3 => '#cdcdb4',
        lightyellow4 => '#8b8b7a',
        linen => '#faf0e6',
        magenta4 => '#8b008b',
        mediumblue => '#0000cd',
        mintcream => '#f5fffa',
        mistyrose => '#ffe4e1',
        mistyrose1 => '#ffe4e1',
        mistyrose2 => '#eed5d2',
        mistyrose3 => '#cdb7b5',
        mistyrose4 => '#8b7d7b',
        moccasin => '#ffe4b5',
        navajowhite => '#ffdead',
        navajowhite1 => '#ffdead',
        navajowhite2 => '#eecfa1',
        navajowhite3 => '#cdb38b',
        navajowhite4 => '#8b795e',
        navy => '#000080',
        navyblue => '#000080',
        oldlace => '#fdf5e6',
        olivedrab => '#6b8e23',
        olivedrab1 => '#c0ff3e',
        olivedrab2 => '#b3ee3a',
        olivedrab3 => '#9acd32',
        olivedrab4 => '#698b22',
        orange => '#ffa500',
        orange1 => '#ffa500',
        orange2 => '#ee9a00',
        orange3 => '#cd8500',
        orange4 => '#8b5a00',
        orangered => '#ff4500',
        orangered1 => '#ff4500',
        orangered2 => '#ee4000',
        orangered3 => '#cd3700',
        orangered4 => '#8b2500',
        orchid => '#da70d6',
        orchid1 => '#ff83fa',
        orchid2 => '#ee7ae9',
        orchid3 => '#cd69c9',
        orchid4 => '#8b4789',
        palegoldenrod => '#eee8aa',
        palegreen => '#98fb98',
        palegreen1 => '#9aff9a',
        palegreen2 => '#90ee90',
        palegreen3 => '#7ccd7c',
        palegreen4 => '#548b54',
        paleturquoise => '#afeeee',
        paleturquoise1 => '#bbffff',
        paleturquoise2 => '#aeeeee',
        papayawhip => '#ffefd5',
        peachpuff => '#ffdab9',
        peachpuff1 => '#ffdab9',
        peachpuff2 => '#eecbad',
        peachpuff3 => '#cdaf95',
        peachpuff4 => '#8b7765',
        peru => '#cd853f',
        pink1 => '#ffb5c5',
        powderblue => '#b0e0e6',
        red3 => '#cd0000',
        red4 => '#8b0000',
        saddlebrown => '#8b4513',
        salmon => '#fa8072',
        seashell => '#fff5ee',
        seashell1 => '#fff5ee',
        seashell2 => '#eee5de',
        seashell3 => '#cdc5bf',
        seashell4 => '#8b8682',
        slateblue1 => '#836fff',
        slateblue3 => '#6959cd',
        slategrey => '#708090',
        slategrey1 => '#c6e2ff',
        slategrey2 => '#b9d3ee',
        slategrey3 => '#9fb6cd',
        slategrey4 => '#6c7b8b',
        snow => '#fffafa',
        snow1 => '#fffafa',
        snow2 => '#eee9e9',
        snow3 => '#cdc9c9',
        snow4 => '#8b8989',
        tan => '#d2b48c',
        tan1 => '#ffa54f',
        tan2 => '#ee9a49',
        tan3 => '#cd853f',
        tan4 => '#8b5a2b',
        tomato => '#ff6347',
        tomato1 => '#ff6347',
        tomato2 => '#ee5c42',
        tomato3 => '#cd4f39',
        tomato4 => '#8b3626',
        turquoise4 => '#00868b',
        wheat => '#f5deb3',
        wheat1 => '#ffe7ba',
        wheat2 => '#eed8ae',
        wheat3 => '#cdba96',
        wheat4 => '#8b7e66',
        whitesmoke => '#f5f5f5',
        yellow2 => '#eeee00',
        yellow3 => '#cdcd00',
        yellow4 => '#8b8b00',
    );


my %name2rgb = map { $_ => [ map { hex( $_ ) / 255 }
                             $name2html{ $_ } =~ m/^#([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/
                           ]
                   }
               keys %name2html;

#  Convert html to RGB:

#  namedcolor
#  #xxxxxx
#  #xxx
#  xxxxxx
#  xxx

sub html2rgb {
    my $html = lc shift;     #  Lower case
    $html =~ s/\s+//g;       #  No spaces
    $html =~ s/grey/gray/g;  #  USA spelling

    my $rgb;
    if ( $rgb = $name2rgb{ $html } ) { return wantarray ? @$rgb : $rgb }

    my @rgb = map { hex( $_ ) / 255 }
              $html =~ m/^#([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/ ? ( $1, $2, $3 )
            : $html =~ m/^#([\da-f])([\da-f])([\da-f])/                      ? map { "$_$_" } ( $1, $2, $3 )
            : $html =~ m/^([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/  ? ( $1, $2, $3 )
            : $html =~ m/^([\da-f])([\da-f])([\da-f])/                       ? map { "$_$_" } ( $1, $2, $3 )
            :                                                                 ( '7f', '7f', '7f' );

    wantarray() ? @rgb : \@rgb
}


#  Produce the gray equivalent in brightness to an RGB value:

sub rgb2gray {
    my ( $r, $g, $b ) = @_;
    if ( ref( $r ) eq "ARRAY" ) { ( $r, $g, $b ) = @$r }
    defined( $r ) && defined( $g ) && defined( $b ) || return undef;
    if ( $r > 1 ) { $r = 1 } elsif ( $r < 0 ) { $r = 0 }
    if ( $g > 1 ) { $g = 1 } elsif ( $g < 0 ) { $g = 0 }
    if ( $b > 1 ) { $b = 1 } elsif ( $b < 0 ) { $b = 0 }
    #  Various ITU recommendations and the new standard:
    # 0.299 * $r + 0.587 * $g + 0.114 * $b;  #  Rec 601-1
    # 0.213 * $r + 0.715 * $g + 0.072 * $b;  #  Rec 709
    0.222 * $r + 0.707 * $g + 0.071 * $b;  #  ITU std (D65 white point)
}


#  Convert an RGB value to an HTML string:

sub rgb2html {
    my ( $r, $g, $b ) = @_;
    if ( ref( $r ) eq "ARRAY" ) { ( $r, $g, $b ) = @$r }
    defined( $r ) && defined( $g ) && defined( $b ) || return undef;
    if ( $r > 1 ) { $r = 1 } elsif ( $r < 0 ) { $r = 0 }
    if ( $g > 1 ) { $g = 1 } elsif ( $g < 0 ) { $g = 0 }
    if ( $b > 1 ) { $b = 1 } elsif ( $b < 0 ) { $b = 0 }
    sprintf("#%02x%02x%02x", int(255.999*$r), int(255.999*$g), int(255.999*$b) )
}


#  Convert an gray value to an HTML string:

sub gray2html {
    my ( $gray ) = @_;
    if ( $gray > 1 ) { $gray = 1 } elsif ( $gray < 0 ) { $gray = 0 }
    my $hex = sprintf "%02x", int(255.999*$gray);
    join( '', '#', $hex, $hex, $hex )
}


#-------------------------------------------------------------------------------
#  Blend 2 or more RGB colors:
#
#     $color = blend_rgb_colors( $color1, $color2, ... )
#-------------------------------------------------------------------------------
sub blend_rgb_colors
{
    my @rgb = ( 0, 0, 0 );
    my $n = 0;
    while ( @_ )
    {
        my @clr = @{ shift @_ };
        @rgb = map { $_ + shift @clr } @rgb;
        $n++;
    }
    @rgb = map { $_/$n } @rgb if $n;
    wantarray() ? @rgb : \@rgb
}


#-------------------------------------------------------------------------------
#  Blend 2 or more HTML colors:
#
#     $color = blend_html_colors( $color1, $color2, ... )
#-------------------------------------------------------------------------------
sub blend_html_colors
{
    my @rgb = ( 0, 0, 0 );
    my $n = 0;
    while ( @_ )
    {
        my @clr = html2rgb( shift );
        @rgb = map { $_ + shift @clr } @rgb;
        $n++;
    }
    @rgb = map { $_/$n } @rgb if $n;

    rgb2html( @rgb );
}


#-------------------------------------------------------------------------------
#  Define a distance between 2 rgb colors
#
#    $name = rgb_distance( $rgb1, $rgb2 )
#-------------------------------------------------------------------------------
sub rgb_distance
{
    ( ref( $_[0] ) eq 'ARRAY' ) && ( ref( $_[0] ) eq 'ARRAY' ) || return undef;
    my @rgb1 = @{$_[0]};
    my @rgb2 = @{$_[1]};
    ( grep { defined } @rgb1, @rgb2 ) == 6 || return undef;
    my $d2 = ( $rgb1[0] - $rgb2[0] ) ** 2
           + ( $rgb1[1] - $rgb2[1] ) ** 2
           + ( $rgb1[2] - $rgb2[2] ) ** 2;
    return sqrt( $d2 );
}


#-------------------------------------------------------------------------------
#  Find the closest named color
#
#    $name = rgb2name( $rgb )
#-------------------------------------------------------------------------------
sub rgb2name
{
    my @rgb = ref( $_[0] ) eq 'ARRAY' ? @{ $_[0] } : @_;
    ( grep { defined } @rgb ) == 3 || return undef;

    my ( $name ) = map  { $_->[0] }
                   sort { $a->[1] <=> $b->[1] }
                   map  { [ $_, rgb_distance( \@rgb, $name2rgb{ $_ } ) ] }
                   keys %name2rgb;
    return $name;
}


1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3