Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Hex to RGB? (perl)

Tags:

hex

rgb

perl

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?

like image 421
Izaak Williamson Avatar asked Nov 30 '22 03:11

Izaak Williamson


1 Answers

Assuming you're trying to map 00..FF16 to real numbers 0..1,

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

  • pack 'H*', changes "FF00FF" to "\xFF\x00\xFF".
  • unpack 'C*', changes "\xFF\x00\xFF" to 0xFF, 0x00, 0xFF.
  • map $_ / 255, changes 0xFF, 0x00, 0xFF to 0xFF/255, 0x00/255, 0xFF/255
like image 164
ikegami Avatar answered Dec 05 '22 06:12

ikegami