function GetHex (decimal : int) : String
Converts int between 0 and 15 to hexidecimal character.
function HexToInt (hexChar : char) : int
Converts single hexadecimal character (0…F) to the corresponding int.
function RGBToHex (color : Color) : String
Convert a Unity color to hexadecimal. NOTE: this function assumes that color values are in the 0…1 range. Alpha values are ignored.
function HexToRGB (color : String) : Color
Convert standard web-formatted (hexadecimal) color (000000…FFFFFF) to RGB color with values in 0…1 range.
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[1]) + HexToInt(color[0]) * 16.000) / 255;
green = (HexToInt(color[3]) + HexToInt(color[2]) * 16.000) / 255;
blue = (HexToInt(color[5]) + HexToInt(color[4]) * 16.000) / 255;
var finalColor = new Color();
finalColor.r = red;
finalColor.g = green;
finalColor.b = blue;
finalColor.a = 1;
return finalColor;
};
来源:http://wiki.unity3d.com/index.php?title=HexConverter
Unity中的拓展可以看这里:ColorExt