ColorExtensions.cs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. using Avalonia.Media;
  2. namespace SukiUI.Extensions;
  3. public static class ColorExtensions
  4. {
  5. public static Color WithAlpha(this Color c, byte alpha) =>
  6. new(alpha, c.R, c.G, c.B);
  7. public static Color WithAlpha(this Color c, double alpha) =>
  8. WithAlpha(c, (byte)(255 * alpha));
  9. /// <summary>
  10. /// Used primarily for SukiEffect runtime effect uniforms, converts a standard Color to an RGB float array in the range 0-1
  11. /// </summary>
  12. public static float[] ToFloatArray(this Color c) =>
  13. new[] { c.R / 255f, c.G / 255f, c.B / 255f };
  14. /// <summary>
  15. /// Used primarily for SukiEffect runtime effect uniforms, converts a standard Color to an RGB float array in the range 0-1.
  16. /// Allows recycling of an array for performance.
  17. /// </summary>
  18. /// <param name="c"></param>
  19. /// <param name="array"></param>
  20. public static void ToFloatArrayNonAlloc(this Color c, float[] array)
  21. {
  22. array[0] = c.R / 255f;
  23. array[1] = c.G / 255f;
  24. array[2] = c.B / 255f;
  25. }
  26. }