Created
February 21, 2018 15:51
-
-
Save glebov21/20dc24da21d497282512ab74df771ac2 to your computer and use it in GitHub Desktop.
Color conberter rgb hsv int hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region COLOR | |
static public Color ParseColor(string text, int offset) { return ParseColor24(text, offset); } | |
static public Color ParseColor24(string text, int offset) | |
{ | |
int r = (HexToDecimal(text[offset]) << 4) | HexToDecimal(text[offset + 1]); | |
int g = (HexToDecimal(text[offset + 2]) << 4) | HexToDecimal(text[offset + 3]); | |
int b = (HexToDecimal(text[offset + 4]) << 4) | HexToDecimal(text[offset + 5]); | |
float f = 1f / 255f; | |
return new Color(f * r, f * g, f * b); | |
} | |
static public int HexToDecimal(char ch) | |
{ | |
switch (ch) | |
{ | |
case '0': return 0x0; | |
case '1': return 0x1; | |
case '2': return 0x2; | |
case '3': return 0x3; | |
case '4': return 0x4; | |
case '5': return 0x5; | |
case '6': return 0x6; | |
case '7': return 0x7; | |
case '8': return 0x8; | |
case '9': return 0x9; | |
case 'a': | |
case 'A': return 0xA; | |
case 'b': | |
case 'B': return 0xB; | |
case 'c': | |
case 'C': return 0xC; | |
case 'd': | |
case 'D': return 0xD; | |
case 'e': | |
case 'E': return 0xE; | |
case 'f': | |
case 'F': return 0xF; | |
} | |
return 0xF; | |
} | |
public static Color SetColorWithoutAlpha(Color destinationColor, Color sourceColor) | |
{ | |
return new Color(sourceColor.r, sourceColor.g, sourceColor.b, destinationColor.a); | |
} | |
public static Color SetAlpha(Color destinationColor, float alpha) | |
{ | |
return new Color(destinationColor.r, destinationColor.g, destinationColor.b, alpha); | |
} | |
public static Color ParseColor(string col) | |
{ | |
//Takes strings formatted with numbers and no spaces before or after the commas: | |
// "1.0,1.0,.35,1.0" | |
//Debug.Log(col); | |
var strings = col.Split(","[0]); | |
Color output = Color.white; | |
for (var i = 0; i < 4; i++) | |
{ | |
output[i] = Single.Parse(strings[i], | |
System.Globalization.NumberStyles.Any, | |
CultureInfo.InvariantCulture); | |
} | |
return output; | |
} | |
public static string GetColorString(Color color) | |
{ | |
string[] cols = new string[4]; | |
for (var i = 0; i < 4; i++) | |
{ | |
cols[i] = color[i].ToString().Replace(',', '.'); | |
} | |
var output = string.Join(",", cols); | |
return output; | |
} | |
/// <summary> | |
/// Сделать текст определенного цвета | |
/// </summary> | |
/// <param name="text">текст</param> | |
/// <param name="color">цвет ("FFAA88")</param> | |
public static string AddColorToText(string text, Color color) | |
{ | |
//<color=#00ffffff> | |
if (text.IndexOf("<color") == 0) //если уже есть цвет, то заменить | |
{ | |
text = text.Remove(0, "<color=#aaaaaaff>".Length); | |
return string.Format("<color=#{1}ff>{0}", text, EncodeColor(color)); | |
} | |
else | |
return string.Format("<color=#{1}ff>{0}</color>", text, EncodeColor(color)); | |
} | |
static public string EncodeColor(Color c) { return EncodeColor24(c); } | |
static public string EncodeColor24(Color c) | |
{ | |
int i = 0xFFFFFF & (ColorToInt(c) >> 8); | |
return DecimalToHex24(i); | |
} | |
static public int ColorToInt(Color c) | |
{ | |
int retVal = 0; | |
retVal |= Mathf.RoundToInt(c.r * 255f) << 24; | |
retVal |= Mathf.RoundToInt(c.g * 255f) << 16; | |
retVal |= Mathf.RoundToInt(c.b * 255f) << 8; | |
retVal |= Mathf.RoundToInt(c.a * 255f); | |
return retVal; | |
} | |
static public string DecimalToHex24(int num) | |
{ | |
num &= 0xFFFFFF; | |
#if UNITY_FLASH | |
StringBuilder sb = new StringBuilder(); | |
sb.Append(DecimalToHexChar((num >> 20) & 0xF)); | |
sb.Append(DecimalToHexChar((num >> 16) & 0xF)); | |
sb.Append(DecimalToHexChar((num >> 12) & 0xF)); | |
sb.Append(DecimalToHexChar((num >> 8) & 0xF)); | |
sb.Append(DecimalToHexChar((num >> 4) & 0xF)); | |
sb.Append(DecimalToHexChar(num & 0xF)); | |
return sb.ToString(); | |
#else | |
return num.ToString("X6"); | |
#endif | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="color"></param> | |
/// <param name="h"></param> | |
/// <param name="s"></param> | |
/// <param name="v"></param> | |
public static void RGBToHSV(Color color, out float h, out float s, out float v) | |
{ | |
float min = Mathf.Min(Mathf.Min(color.r, color.g), color.b); | |
float max = Mathf.Max(Mathf.Max(color.r, color.g), color.b); | |
float delta = max - min; | |
// value is our max color | |
v = max; | |
// saturation is percent of max | |
if (!Mathf.Approximately(max, 0)) | |
s = delta / max; | |
else | |
{ | |
// all colors are zero, no saturation and hue is undefined | |
s = 0; | |
h = -1; | |
return; | |
} | |
// grayscale image if min and max are the same | |
if (Mathf.Approximately(min, max)) | |
{ | |
v = max; | |
s = 0; | |
h = -1; | |
return; | |
} | |
// hue depends which color is max (this creates a rainbow effect) | |
if (color.r == max) | |
h = (color.g - color.b) / delta; // between yellow & magenta | |
else if (color.g == max) | |
h = 2 + (color.b - color.r) / delta; // between cyan & yellow | |
else | |
h = 4 + (color.r - color.g) / delta; // between magenta & cyan | |
// turn hue into 0-360 degrees | |
h *= 60; | |
if (h < 0) | |
h += 360; | |
} | |
public static float GetScreenDPI() | |
{ | |
var result = Screen.dpi; | |
if (result < 2f) | |
result = 96f; | |
return result; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="h">[0-360]</param> | |
/// <param name="s">[0-1]</param> | |
/// <param name="v">[0-1]</param> | |
/// <returns></returns> | |
public static Color HSVtoRGB(float h, float s, float v) | |
{ | |
// no saturation, we can return the value across the board (grayscale) | |
if (s == 0) | |
return new Color(v, v, v); | |
// which chunk of the rainbow are we in? | |
float sector = h / 60; | |
// split across the decimal (ie 3.87 into 3 and 0.87) | |
int i = (int)sector; | |
float f = sector - i; | |
float p = v * (1 - s); | |
float q = v * (1 - s * f); | |
float t = v * (1 - s * (1 - f)); | |
// build our rgb color | |
Color color = new Color(0, 0, 0); | |
switch (i) | |
{ | |
case 0: | |
color.r = v; | |
color.g = t; | |
color.b = p; | |
break; | |
case 1: | |
color.r = q; | |
color.g = v; | |
color.b = p; | |
break; | |
case 2: | |
color.r = p; | |
color.g = v; | |
color.b = t; | |
break; | |
case 3: | |
color.r = p; | |
color.g = q; | |
color.b = v; | |
break; | |
case 4: | |
color.r = t; | |
color.g = p; | |
color.b = v; | |
break; | |
default: | |
color.r = v; | |
color.g = p; | |
color.b = q; | |
break; | |
} | |
return color; | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment