4

How do I convert my hexidecimal colors (e.g., 0000FF, FF00FF) into arithmetic RGB format (e.g., 0 0 1, 1 0 1)?

I want to implement a command to do this in some of my perl scripts but I don't even know how to do it by hand.

Could someone please help me do this in perl or even show me how to do this by hand so I can come up with my own perl command?

4

2 回答 2

10

已经有一个 CPAN 模块可以满足您的需求:https ://metacpan.org/pod/Color::Rgb

use Color::Rgb;
my $hex = '#0000FF';

my @rgb        = $rgb->hex2rgb($hex);     # returns list of 0, 0, 255
my $rgb_string = $rgb->hex2rgb($hex,','); # returns string '0,0,255'

它也可以朝另一个方向发展:

my @rgb        = (0, 0, 255);              
my $hex_string = $rgb->rgb2hex(@rgb);     # returns '0000FF'
于 2013-07-11T20:24:45.547 回答
10

假设您正在尝试将 00..FF 16映射到实数 0..1,

my @rgb = map $_ / 255, unpack 'C*', pack 'H*', $rgb_hex;

  • pack 'H*',更改"FF00FF""\xFF\x00\xFF".
  • unpack 'C*',更改"\xFF\x00\xFF"0xFF, 0x00, 0xFF.
  • map $_ / 255,更改0xFF, 0x00, 0xFF0xFF/255, 0x00/255, 0xFF/255
于 2013-07-11T20:42:25.620 回答