/// <summary> /// Creates a new color based on a Hsva value. /// </summary> /// <param name="hsva"></param> /// <returns></returns> public static ColorRgba FromHsva(ColorHsva hsva) { return(hsva.ToRgba()); }
/// <summary> /// Adjusts the color to match the specified Hsva color. /// </summary> /// <param name="hsva"></param> public void SetHsva(ColorHsva hsva) { this = hsva.ToRgba(); }
/// <summary> /// Retrieves a copy of the values that are assigned the specified variable. If the internally /// stored type does not match the specified type, it will be converted before returning. /// /// Supported base types are <see cref="float"/>, <see cref="Vector2"/>, <see cref="Vector3"/>, /// <see cref="Vector4"/>, <see cref="Matrix3"/>, <see cref="Matrix4"/>, <see cref="int"/>, /// <see cref="Point2"/> and <see cref="bool"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <returns></returns> public bool TryGet <T>(string name, out T[] value) where T : struct { value = null; if (string.IsNullOrEmpty(name)) { return(false); } float[] rawData; if (!this.TryGetInternal(name, out rawData)) { return(false); } if (typeof(T) == typeof(float)) { if (rawData.Length < 1) { return(false); } float[] result = new float[rawData.Length / 1]; for (int i = 0; i < result.Length; i++) { result[i] = rawData[i]; } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(Vector2)) { if (rawData.Length < 2) { return(false); } Vector2[] result = new Vector2[rawData.Length / 2]; for (int i = 0; i < result.Length; i++) { result[i].X = rawData[i * 2 + 0]; result[i].Y = rawData[i * 2 + 1]; } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(Vector3)) { if (rawData.Length < 3) { return(false); } Vector3[] result = new Vector3[rawData.Length / 3]; for (int i = 0; i < result.Length; i++) { result[i].X = rawData[i * 3 + 0]; result[i].Y = rawData[i * 3 + 1]; result[i].Z = rawData[i * 3 + 2]; } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(Vector4)) { if (rawData.Length < 4) { return(false); } Vector4[] result = new Vector4[rawData.Length / 4]; for (int i = 0; i < result.Length; i++) { result[i].X = rawData[i * 4 + 0]; result[i].Y = rawData[i * 4 + 1]; result[i].Z = rawData[i * 4 + 2]; result[i].W = rawData[i * 4 + 3]; } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(Matrix3)) { if (rawData.Length < 9) { return(false); } Matrix3[] result = new Matrix3[rawData.Length / 9]; for (int i = 0; i < result.Length; i++) { result[i].Row0.X = rawData[i * 9 + 0]; result[i].Row0.Y = rawData[i * 9 + 1]; result[i].Row0.Z = rawData[i * 9 + 2]; result[i].Row1.X = rawData[i * 9 + 3]; result[i].Row1.Y = rawData[i * 9 + 4]; result[i].Row1.Z = rawData[i * 9 + 5]; result[i].Row2.X = rawData[i * 9 + 6]; result[i].Row2.Y = rawData[i * 9 + 7]; result[i].Row2.Z = rawData[i * 9 + 8]; } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(Matrix4)) { if (rawData.Length < 16) { return(false); } Matrix4[] result = new Matrix4[rawData.Length / 16]; for (int i = 0; i < result.Length; i++) { result[i].Row0.X = rawData[i * 16 + 0]; result[i].Row0.Y = rawData[i * 16 + 1]; result[i].Row0.Z = rawData[i * 16 + 2]; result[i].Row0.W = rawData[i * 16 + 3]; result[i].Row1.X = rawData[i * 16 + 4]; result[i].Row1.Y = rawData[i * 16 + 5]; result[i].Row1.Z = rawData[i * 16 + 6]; result[i].Row1.W = rawData[i * 16 + 7]; result[i].Row2.X = rawData[i * 16 + 8]; result[i].Row2.Y = rawData[i * 16 + 9]; result[i].Row2.Z = rawData[i * 16 + 10]; result[i].Row2.W = rawData[i * 16 + 11]; result[i].Row3.X = rawData[i * 16 + 12]; result[i].Row3.Y = rawData[i * 16 + 13]; result[i].Row3.Z = rawData[i * 16 + 14]; result[i].Row3.W = rawData[i * 16 + 15]; } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(ColorRgba)) { if (rawData.Length < 4) { return(false); } ColorRgba[] result = new ColorRgba[rawData.Length / 4]; for (int i = 0; i < result.Length; i++) { result[i] = new ColorRgba( rawData[i * 4 + 0], rawData[i * 4 + 1], rawData[i * 4 + 2], rawData[i * 4 + 3]); } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(ColorHsva)) { if (rawData.Length < 4) { return(false); } ColorHsva[] result = new ColorHsva[rawData.Length / 4]; for (int i = 0; i < result.Length; i++) { result[i] = new ColorHsva( rawData[i * 4 + 0], rawData[i * 4 + 1], rawData[i * 4 + 2], rawData[i * 4 + 3]); } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(int)) { if (rawData.Length < 1) { return(false); } int[] result = new int[rawData.Length / 1]; for (int i = 0; i < result.Length; i++) { result[i] = MathF.RoundToInt(rawData[i]); } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(Point2)) { if (rawData.Length < 2) { return(false); } Point2[] result = new Point2[rawData.Length / 2]; for (int i = 0; i < result.Length; i++) { result[i].X = MathF.RoundToInt(rawData[i * 2 + 0]); result[i].Y = MathF.RoundToInt(rawData[i * 2 + 1]); } value = (T[])(object)result; return(true); } else if (typeof(T) == typeof(bool)) { if (rawData.Length < 1) { return(false); } bool[] result = new bool[rawData.Length / 1]; for (int i = 0; i < result.Length; i++) { result[i] = rawData[i] != 0.0f; } value = (T[])(object)result; return(true); } else { ThrowUnsupportedValueType <T>(); return(false); } }
/// <summary> /// Converts the color to Hsva. /// </summary> /// <returns></returns> public ColorHsva ToHsva() { return(ColorHsva.FromRgba(this)); }
/// <summary> /// Assigns a blittable value to the specified variable. All values are copied and converted into /// a shared internal format. /// /// Supported base types are <see cref="float"/>, <see cref="Vector2"/>, <see cref="Vector3"/>, /// <see cref="Vector4"/>, <see cref="Matrix3"/>, <see cref="Matrix4"/>, <see cref="int"/>, /// <see cref="Point2"/> and <see cref="bool"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <param name="value"></param> public void Set <T>(string name, T value) where T : struct { if (string.IsNullOrEmpty(name)) { ThrowInvalidName(); } float[] rawData; if (typeof(T) == typeof(float)) { float typedValue = (float)(object)value; this.EnsureUniformData(name, 1, out rawData); rawData[0] = typedValue; } else if (typeof(T) == typeof(Vector2)) { Vector2 typedValue = (Vector2)(object)value; this.EnsureUniformData(name, 2, out rawData); rawData[0] = typedValue.X; rawData[1] = typedValue.Y; } else if (typeof(T) == typeof(Vector3)) { Vector3 typedValue = (Vector3)(object)value; this.EnsureUniformData(name, 3, out rawData); rawData[0] = typedValue.X; rawData[1] = typedValue.Y; rawData[2] = typedValue.Z; } else if (typeof(T) == typeof(Vector4)) { Vector4 typedValue = (Vector4)(object)value; this.EnsureUniformData(name, 4, out rawData); rawData[0] = typedValue.X; rawData[1] = typedValue.Y; rawData[2] = typedValue.Z; rawData[3] = typedValue.W; } else if (typeof(T) == typeof(Matrix3)) { Matrix3 typedValue = (Matrix3)(object)value; this.EnsureUniformData(name, 9, out rawData); rawData[0] = typedValue.Row0.X; rawData[1] = typedValue.Row0.Y; rawData[2] = typedValue.Row0.Z; rawData[3] = typedValue.Row1.X; rawData[4] = typedValue.Row1.Y; rawData[5] = typedValue.Row1.Z; rawData[6] = typedValue.Row2.X; rawData[7] = typedValue.Row2.Y; rawData[8] = typedValue.Row2.Z; } else if (typeof(T) == typeof(Matrix4)) { Matrix4 typedValue = (Matrix4)(object)value; this.EnsureUniformData(name, 16, out rawData); rawData[0] = typedValue.Row0.X; rawData[1] = typedValue.Row0.Y; rawData[2] = typedValue.Row0.Z; rawData[3] = typedValue.Row0.W; rawData[4] = typedValue.Row1.X; rawData[5] = typedValue.Row1.Y; rawData[6] = typedValue.Row1.Z; rawData[7] = typedValue.Row1.W; rawData[8] = typedValue.Row2.X; rawData[9] = typedValue.Row2.Y; rawData[10] = typedValue.Row2.Z; rawData[11] = typedValue.Row2.W; rawData[12] = typedValue.Row3.X; rawData[13] = typedValue.Row3.Y; rawData[14] = typedValue.Row3.Z; rawData[15] = typedValue.Row3.W; } else if (typeof(T) == typeof(ColorRgba)) { ColorRgba typedValue = (ColorRgba)(object)value; this.EnsureUniformData(name, 4, out rawData); rawData[0] = (float)typedValue.R / 255.0f; rawData[1] = (float)typedValue.G / 255.0f; rawData[2] = (float)typedValue.B / 255.0f; rawData[3] = (float)typedValue.A / 255.0f; } else if (typeof(T) == typeof(ColorHsva)) { ColorHsva typedValue = (ColorHsva)(object)value; this.EnsureUniformData(name, 4, out rawData); rawData[0] = typedValue.H; rawData[1] = typedValue.S; rawData[2] = typedValue.V; rawData[3] = typedValue.A; } else if (typeof(T) == typeof(int)) { int typedValue = (int)(object)value; this.EnsureUniformData(name, 1, out rawData); rawData[0] = typedValue; } else if (typeof(T) == typeof(Point2)) { Point2 typedValue = (Point2)(object)value; this.EnsureUniformData(name, 2, out rawData); rawData[0] = typedValue.X; rawData[1] = typedValue.Y; } else if (typeof(T) == typeof(bool)) { bool typedValue = (bool)(object)value; this.EnsureUniformData(name, 1, out rawData); rawData[0] = typedValue ? 1.0f : 0.0f; } else { ThrowUnsupportedValueType <T>(); } this.UpdateHash(); }