Difference between revisions of "Convert RGB value to Hexadecimal (to be used in HTML for example)"

From CodeCodex

(Java)
 
(Implementations)
 
Line 1: Line 1:
 
==Implementations==  
 
==Implementations==  
 
===Java===  
 
===Java===  
<highlightsyntax language="java122">
+
<pre class="java">
 
import java.awt.*;
 
import java.awt.*;
 
public class Color2Hex {
 
public class Color2Hex {
Line 19: Line 19:
 
   }
 
   }
 
}
 
}
</highlightsyntax>
+
</pre>
 +
 
 +
=== Tcl ===
 +
<pre class="tcl">
 +
proc f2i col {return [expr int($col * 255.0)]}
 +
proc rgb2hex { r g b } {return [format "#%02X%02X%02X" [f2i $r] [f2i $g] [f2i $b]]}
 +
</pre>
 +
 
 
[[Category:Java]]
 
[[Category:Java]]
 +
[[Category:Tcl]]

Latest revision as of 22:13, 19 February 2011

Implementations[edit]

Java[edit]

import java.awt.*;
public class Color2Hex {
  public static void main( String[] args ) {
    if (args.length != 3) {
      System.out.println("Color2Hex  r g b");
      }
   else {
      int i = Integer.parseInt(args[0]);
      int j = Integer.parseInt(args[1]);
      int k = Integer.parseInt(args[2]);
    
      Color c = new Color(i,j,k);
      System.out.println
        ( "hex: " + Integer.toHexString( c.getRGB() & 0x00ffffff ) ); 
      }
   }
}

Tcl[edit]

proc f2i col {return [expr int($col * 255.0)]}
proc rgb2hex { r g b } {return [format "#%02X%02X%02X" [f2i $r] [f2i $g] [f2i $b]]}