颜色Hex和RGB的互转

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iningwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值