Skip to content

Instantly share code, notes, and snippets.

@Frooxius
Created December 17, 2018 17:20
Show Gist options
  • Save Frooxius/e165f4099a9c08ab5f91df860d5ca654 to your computer and use it in GitHub Desktop.
Save Frooxius/e165f4099a9c08ab5f91df860d5ca654 to your computer and use it in GitHub Desktop.
LogiX Type Colors
public static partial class LogixHelper
{
static color UnsignedIntegerColor { get { return new color(0f, 0.5f, 1f); } }
static color SignedIntegerColor { get { return new color(0f, 1f, 0f); } }
static color FloatingPointColor { get { return new color(0f, 1f, 1f); } }
static color BooleanColor { get { return new color(0.25f, 0.25f, 0.25f); } }
static color ColorColor { get { return new color(1f, 0.8f, 0f); } }
static color StringColor { get { return new color(0.5f, 0f, 0f); } }
static color DummyColor { get { return new color(1f, 0f, 1f); } }
static color ImpulseColor { get { return new color(1f, 1f, 1f); } }
static color RangeColor(color color, int rangeOffset)
{
if (rangeOffset == 0)
return color;
if (rangeOffset < 0)
return MathX.Lerp(color, color.Black, -rangeOffset * 0.15f);
return MathX.Lerp(color, color.White, rangeOffset * 0.15f);
}
public static color GetColor(this Type type)
{
if (type == typeof(byte))
return RangeColor(UnsignedIntegerColor, -2);
if (type == typeof(ushort))
return RangeColor(UnsignedIntegerColor, -1);
if (type == typeof(uint))
return RangeColor(UnsignedIntegerColor, 0);
if (type == typeof(ulong))
return RangeColor(UnsignedIntegerColor, 1);
if (type == typeof(sbyte))
return RangeColor(SignedIntegerColor, -2);
if (type == typeof(short))
return RangeColor(SignedIntegerColor, -1);
if (type == typeof(int))
return RangeColor(SignedIntegerColor, 0);
if (type == typeof(long))
return RangeColor(SignedIntegerColor, 1);
if (type == typeof(float))
return RangeColor(FloatingPointColor, -1);
if (type == typeof(double))
return RangeColor(FloatingPointColor, 0);
if (type == typeof(decimal))
return RangeColor(FloatingPointColor, 1);
if (type == typeof(bool))
return BooleanColor;
if (type == typeof(string))
return StringColor;
if (type == typeof(color))
return ColorColor; // ColorColorColorColorClororloloblop
if (type == typeof(Action))
return ImpulseColor;
if (typeof(IVector).IsAssignableFrom(type))
return type.GetVectorBaseType().GetColor();
if (type == typeof(dummy))
return DummyColor;
// Check if it's a content type, in which case extract that
var contentType = GetContentType(type);
if (contentType != null)
return contentType.GetColor();
// Not any known type, generate a color from the hash
var hash = type.Name.GetHashCode();
float hue = ((ushort)hash) / (float)ushort.MaxValue;
// limit maximum saturation to avoid clashes with common types
float saturation = (((byte)(hash >> 16)) / (float)byte.MaxValue) / 2f;
float value = 0.3f + (((byte)(hash >> 24)) / (float)byte.MaxValue) * 0.7f;
return new ColorHSV(hue, saturation, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment