Skip to content

Instantly share code, notes, and snippets.

@cubed2d
Last active August 29, 2015 14:06
Show Gist options
  • Save cubed2d/387e24c3310ca2db3e4a to your computer and use it in GitHub Desktop.
Save cubed2d/387e24c3310ca2db3e4a to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityColor32 = UnityEngine.Color32;
[Serializable]
[StructLayout(LayoutKind.Explicit)] // Tell .net that we know exactly where we want these values
public struct Color32
{
[FieldOffset(0)]
public byte r;
[FieldOffset(1)]
public byte g;
[FieldOffset(2)]
public byte b;
[FieldOffset(3)]
public byte a;
[FieldOffset(0)]
public UnityColor32 UnityColor;
public byte this[int index]
{
get
{
switch (index)
{
case 0:
return this.r;
case 1:
return this.g;
case 2:
return this.b;
case 3:
return this.a;
default:
throw new IndexOutOfRangeException("Invalid Vector4 index!");
}
}
set
{
switch (index)
{
case 0:
this.r = value;
break;
case 1:
this.g = value;
break;
case 2:
this.b = value;
break;
case 3:
this.a = value;
break;
default:
throw new IndexOutOfRangeException("Invalid Vector4 index!");
}
}
}
public Color32(byte r, byte g, byte b, byte a)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
UnityColor = new UnityEngine.Color32();
}
public override string ToString()
{
return String.Format("RGBA({0}, {1}, {2}, {3})", new object[]
{
this.r,
this.g,
this.b,
this.a
});
}
public string ToString(string format)
{
return String.Format("RGBA({0}, {1}, {2}, {3})", new object[]
{
this.r.ToString(format),
this.g.ToString(format),
this.b.ToString(format),
this.a.ToString(format)
});
}
public static Color32 Lerp(Color32 a, Color32 b, float t)
{
t = Mathf.Clamp01(t);
return new Color32((byte)((float)a.r + (float)(b.r - a.r) * t), (byte)((float)a.g + (float)(b.g - a.g) * t), (byte)((float)a.b + (float)(b.b - a.b) * t), (byte)((float)a.a + (float)(b.a - a.a) * t));
}
public static implicit operator Color32(Color c)
{
return new Color32((byte)(Mathf.Clamp01(c.r) * 255f), (byte)(Mathf.Clamp01(c.g) * 255f), (byte)(Mathf.Clamp01(c.b) * 255f), (byte)(Mathf.Clamp01(c.a) * 255f));
}
public static implicit operator Color(Color32 c)
{
return new Color((float)c.r / 255f, (float)c.g / 255f, (float)c.b / 255f, (float)c.a / 255f);
}
public static implicit operator UnityColor32(Color32 c)
{
return c.UnityColor;
}
public static implicit operator Color32 (UnityColor32 c)
{
Color32 result = new Color32();
result.UnityColor = c;
return result;
}
}
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Texture2D Tex;
// Use this for initialization
void Start () {
// put a breakpoint in our Color32's constructor! this totally uses our one omg!
var data = Tex.GetPixels32();
// other quick tests
var color1 = new UnityEngine.Color32(125,125,125,255);
Color32 color2 = color1;
Color color3 = color2;
}
// Update is called once per frame
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment