HexConverter

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
Line 1: Line 1:
'''function GetHex (decimal : int) : String'''<br>
 
Converts int between 0 and 15 to hexidecimal character.<br>
 
'''function HexToInt (hexChar : char) : int'''<br>
 
Converts single hexadecimal character (0..F) to the corresponding int.<br>
 
'''function RGBToHex (color : Color) : String'''<br>
 
Convert a Unity color to hexadecimal. NOTE: this function assumes that color values are in the 0..1 range. Alpha values are ignored.<br>
 
'''function HexToRGB (color : String) : Color'''<br>
 
Convert standard web-formatted (hexadecimal) color (000000..FFFFFF) to RGB color with values in 0..1 range.<br>
 
  
Author: [http://www.unifycommunity.com/wiki/index.php?title=User:DannyL Danny Lawrence]
 
 
<javascript>function GetHex (decimal : int) {
 
alpha = "0123456789ABCDEF";
 
out = "" + alpha[decimal];
 
return out;
 
};
 
 
function HexToInt (hexChar : char) {
 
var hex : String = "" + hexChar;
 
switch (hex) {
 
case "0": return 0;
 
case "1": return 1;
 
case "2": return 2;
 
case "3": return 3;
 
case "4": return 4;
 
case "5": return 5;
 
case "6": return 6;
 
case "7": return 7;
 
case "8": return 8;
 
case "9": return 9;
 
case "A": return 10;
 
case "B": return 11;
 
case "C": return 12;
 
case "D": return 13;
 
case "E": return 14;
 
case "F": return 15;
 
}
 
};
 
 
function RGBToHex (color : Color) {
 
  red = color.r * 255;
 
  green = color.g * 255;
 
  blue = color.b * 255;
 
 
  a = GetHex(Mathf.Floor(red / 16));
 
  b = GetHex(Mathf.Round(red % 16));
 
  c = GetHex(Mathf.Floor(green / 16));
 
  d = GetHex(Mathf.Round(green % 16));
 
  e = GetHex(Mathf.Floor(blue / 16));
 
  f = GetHex(Mathf.Round(blue % 16));
 
 
  z = a + b + c + d + e + f;
 
 
  return z;
 
};
 
 
function HexToRGB (color : String) {
 
red = (HexToInt(color[0]) + HexToInt(color[1]) * 16.000) / 255;
 
green = (HexToInt(color[2]) + HexToInt(color[3]) * 16.000) / 255;
 
blue = (HexToInt(color[4]) + HexToInt(color[5]) * 16.000) / 255;
 
var finalColor = new Color();
 
finalColor.r = red;
 
finalColor.g = green;
 
finalColor.b = blue;
 
finalColor.a = 1;
 
return finalColor;
 
};</javascript>
 

Revision as of 18:41, 19 October 2009

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox