Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert hexadecimal color to decimal RGB values in UNIX shell script

I'm sure someone has already solved this problem: what is an easy and portable way to convert a hex color value (anything from 000000 to FFFFFF) to 3 decimal values from 0 to 255. (If you're not familiar with how colors are traditionally represented in HTML, the first two hex digits are the first decimal number, and so forth.)

like image 691
iconoclast Avatar asked Aug 31 '11 06:08

iconoclast


1 Answers

$ cat hexrgb.sh
#!/bin/bash
hex="11001A"
printf "%d %d %d\n" 0x${hex:0:2} 0x${hex:2:2} 0x${hex:4:2}

$ ./hexrgb.sh
17 0 26

If you are not willing to use bash for substring expansion, I'd still use printf for the conversion.

like image 197
plundra Avatar answered Oct 29 '22 07:10

plundra