7 added 49 characters in body
source | link

Added 2015-10-06: bash (more unorthodox method) 8484 83 chars

c=({0..9} {a..f}) d=\#;for b;do for a in / %;do d+=${c[$b$a 0x10]020]};done;done;echo $d

I know, it could be 82 chars if 0x10020 where written 16, but I prefer hexadecimal therethis... Or maybe d+=${c[$b$a 0x10]} which was first post.

Added 2015-10-06: bash (more unorthodox method) 84 chars

c=({0..9} {a..f}) d=\#;for b;do for a in / %;do d+=${c[$b$a 0x10]};done;done;echo $d

I know, it could be 82 chars if 0x10 where written 16, but I prefer hexadecimal there.

Added 2015-10-06: bash (more unorthodox method) 84 83 chars

c=({0..9} {a..f}) d=\#;for b;do for a in / %;do d+=${c[$b$a 020]};done;done;echo $d

I know, it could be 82 chars if 020 where written 16, but I prefer this... Or maybe d+=${c[$b$a 0x10]} which was first post.

6 added 566 characters in body
source | link

Added 2015-10-06: bash (more unorthodox method) 84 chars

c=({0..9} {a..f}) d=\#;for b;do for a in / %;do d+=${c[$b$a 0x10]};done;done;echo $d

I know, it could be 82 chars if 0x10 where written 16, but I prefer hexadecimal there.

hexcolor() {
    local a b c=({0..9} {a..f}) d=\#
    for b ;do
        for a in / % ;do
            d+=${c[$b$a 0x10]}
        done
    done
    echo $d
}
hexcolor 72 61 139
#483d8b
hexcolor 75 0 130
#4b0082
hexcolor 0 255 127
#00ff7f

Another approach

Another approach

Added 2015-10-06: bash (more unorthodox method) 84 chars

c=({0..9} {a..f}) d=\#;for b;do for a in / %;do d+=${c[$b$a 0x10]};done;done;echo $d

I know, it could be 82 chars if 0x10 where written 16, but I prefer hexadecimal there.

hexcolor() {
    local a b c=({0..9} {a..f}) d=\#
    for b ;do
        for a in / % ;do
            d+=${c[$b$a 0x10]}
        done
    done
    echo $d
}
hexcolor 72 61 139
#483d8b
hexcolor 75 0 130
#4b0082
hexcolor 0 255 127
#00ff7f

Another approach

5 added 306 characters in body
source | link

bash 2222 21 chars

(Thanks @manatwork for 1 char using \ instead of double-quotes)

printf "#%02X%02X%02X"\#%02X%02X%02X 12 12 12 
#0C0C0C

or reading STDIN in a loop: 48 chars:

while read f;do printf "#%02X%02X%02X\n" $f;done <<<$'72 61 139\n75 0 130\n0 255 127'
#483D8B
#4B0082
#00FF7F

Another approach

#!/bin/bash
browser=firefox   #  google-chrome iceweasel
url="data:text/html;charset=UTF-8,<html><head></head><body>"
url+="<script type='text/javascript'>
  function h(i){var h=i.toString(16);if(16>1*i)h='0'+h;
  return h};function C(r,g,b){return'\043'+h(r)+h(g)+h(b)};
  function m(){ var r=1.0*R.value; var g=1.0*G.value; var b=1.0*B.value;
   var fore='black';if(384>r+g+b)fore='white';var c=C(r,g,b);
   s.setAttribute('style','background:'+c+';color:'+fore+';');s.innerHTML=c};
  function w(e){console.log(e);var val=e.target.value;var q=1;if(e.shiftKey)
  q=15;if(e.detail){if(e.detail>0){q=0-q;}}else if(0>e.wheelDelta){q=0-q;};
  val=1*val+q;if(val>255)val=255;if(0>val)val=0;e.target.value=val;m(); };
  function k(e){console.log(e);var val=e.target.value;var q=1;if(e.shiftKey)q=
  15;if(e.keyCode==38){val=1*val+q;if(val>255)val=255;e.target.value=val;m();}
  else if(e.keyCode==40){val=1*val-q;if(0>val)val=0;e.target.value=val;m();}};
  function n(){R=document.getElementById('R');G=document.getElementById('G');
    B=document.getElementById('B');s=document.getElementById('s');
    R.addEventListener('DOMMouseScroll',w);R.addEventListener('mousewheel',w);
    G.addEventListener('DOMMouseScroll',w);G.addEventListener('mousewheel',w);
    B.addEventListener('DOMMouseScroll',w);B.addEventListener('mousewheel',w);
  m();};var R, G, B, s;window.onload=n;
  </script><style>div{display:inline-block;width:10em;}</style>
  <div id='s'>&nbsp;</div>"
input="%s:<input type='text' size='5' value='200'"
input+=" onKeyDown='k(event)' onChange='m()' id='%s' />"
for c in R G B ;do
    printf -v add "$input" $c $c
    url+="$add"
  done    
$browser "$url"

This will display a browser window, with:

RGB int 2 hex viewer converter

Where you could roll the mousewheel to change values (with shift key holded for step by 15)...

bash 22 chars

printf "#%02X%02X%02X" 12 12 12 
#0C0C0C

or reading STDIN in a loop: 48 chars:

while read f;do printf "#%02X%02X%02X\n" $f;done <<<$'72 61 139\n75 0 130\n0 255 127'
#483D8B
#4B0082
#00FF7F

Another approach

#!/bin/bash
browser=firefox   #  google-chrome iceweasel
url="data:text/html;charset=UTF-8,<html><head></head><body>"
url+="<script type='text/javascript'>
  function h(i){var h=i.toString(16);if(16>1*i)h='0'+h;
  return h};function C(r,g,b){return'\043'+h(r)+h(g)+h(b)};
  function m(){ var r=1.0*R.value; var g=1.0*G.value; var b=1.0*B.value;
   var fore='black';if(384>r+g+b)fore='white';var c=C(r,g,b);
   s.setAttribute('style','background:'+c+';color:'+fore+';');s.innerHTML=c};
  function w(e){console.log(e);var val=e.target.value;var q=1;if(e.shiftKey)
  q=15;if(e.detail){if(e.detail>0){q=0-q;}}else if(0>e.wheelDelta){q=0-q;};
  val=1*val+q;if(val>255)val=255;if(0>val)val=0;e.target.value=val;m(); };
  function k(e){console.log(e);var val=e.target.value;var q=1;if(e.shiftKey)q=
  15;if(e.keyCode==38){val=1*val+q;if(val>255)val=255;e.target.value=val;m();}
  else if(e.keyCode==40){val=1*val-q;if(0>val)val=0;e.target.value=val;m();}};
  function n(){R=document.getElementById('R');G=document.getElementById('G');
    B=document.getElementById('B');s=document.getElementById('s');
    R.addEventListener('DOMMouseScroll',w);R.addEventListener('mousewheel',w);
    G.addEventListener('DOMMouseScroll',w);G.addEventListener('mousewheel',w);
    B.addEventListener('DOMMouseScroll',w);B.addEventListener('mousewheel',w);
  m();};var R, G, B, s;window.onload=n;
  </script><style>div{display:inline-block;width:10em;}</style>
  <div id='s'>&nbsp;</div>"
input="%s:<input type='text' size='5' value='200'"
input+=" onKeyDown='k(event)' onChange='m()' id='%s' />"
for c in R G B ;do
    printf -v add "$input" $c $c
    url+="$add"
  done    
$browser "$url"

bash 22 21 chars

(Thanks @manatwork for 1 char using \ instead of double-quotes)

printf \#%02X%02X%02X 12 12 12 
#0C0C0C

or reading STDIN in a loop: 48 chars:

while read f;do printf "#%02X%02X%02X\n" $f;done <<<$'72 61 139\n75 0 130\n0 255 127'
#483D8B
#4B0082
#00FF7F

Another approach

#!/bin/bash
browser=firefox   #  google-chrome iceweasel
url="data:text/html;charset=UTF-8,<html><head></head><body>"
url+="<script type='text/javascript'>
  function h(i){var h=i.toString(16);if(16>1*i)h='0'+h;
  return h};function C(r,g,b){return'\043'+h(r)+h(g)+h(b)};
  function m(){ var r=1.0*R.value; var g=1.0*G.value; var b=1.0*B.value;
   var fore='black';if(384>r+g+b)fore='white';var c=C(r,g,b);
   s.setAttribute('style','background:'+c+';color:'+fore+';');s.innerHTML=c};
  function w(e){console.log(e);var val=e.target.value;var q=1;if(e.shiftKey)
  q=15;if(e.detail){if(e.detail>0){q=0-q;}}else if(0>e.wheelDelta){q=0-q;};
  val=1*val+q;if(val>255)val=255;if(0>val)val=0;e.target.value=val;m(); };
  function k(e){console.log(e);var val=e.target.value;var q=1;if(e.shiftKey)q=
  15;if(e.keyCode==38){val=1*val+q;if(val>255)val=255;e.target.value=val;m();}
  else if(e.keyCode==40){val=1*val-q;if(0>val)val=0;e.target.value=val;m();}};
  function n(){R=document.getElementById('R');G=document.getElementById('G');
    B=document.getElementById('B');s=document.getElementById('s');
    R.addEventListener('DOMMouseScroll',w);R.addEventListener('mousewheel',w);
    G.addEventListener('DOMMouseScroll',w);G.addEventListener('mousewheel',w);
    B.addEventListener('DOMMouseScroll',w);B.addEventListener('mousewheel',w);
  m();};var R, G, B, s;window.onload=n;
  </script><style>div{display:inline-block;width:10em;}</style>
  <div id='s'>&nbsp;</div>"
input="%s:<input type='text' size='5' value='200'"
input+=" onKeyDown='k(event)' onChange='m()' id='%s' />"
for c in R G B ;do
    printf -v add "$input" $c $c
    url+="$add"
  done    
$browser "$url"

This will display a browser window, with:

RGB int 2 hex viewer converter

Where you could roll the mousewheel to change values (with shift key holded for step by 15)...

4 Add mouse wheel support
source | link
3 Add another bash approach
source | link
2 added 14 characters in body
source | link
1
source | link