Color RGB int to hex

Ruby

I was going more for the "unorthodox" part. ;)

10;a=    gets.   split
.  map  {|       x|  #x
"%02X"  %(  x.   to_i)
}; $>   <<   ?#  <<  a[
00  ..   1+2].   join;

The font (on my web browser) is a bit tall, so it looks distorted, but it looks better in Courier New:

image

Sample run:

c:\a\ruby>rgb2hex
255 100 0
#FF6400

Ruby: 19 characters

$_=?#+"%02X"*3%$F

Sample run:

bash-4.1$ ruby -pae '$_=?#+"%02X"*3%$F' <<< '72 61 139'
#483D8B

bash-4.1$ ruby -pae '$_=?#+"%02X"*3%$F' <<< '75 0 130'
#4B0082

bash-4.1$ ruby -pae '$_=?#+"%02X"*3%$F' <<< '0 255 127'
#00FF7F

q/k (3 chars)

Not exactly unorthodox but quite short

4h$

Example

q)4h$ 72 61 139
0x483d8b

To match the output format in the question exactly, we can do (for 9 chars):

"#",/$4h$

Tags:

Code Golf