|
| 1 | +// Copyright (c) 2019-2025 ReactiveUI Association Incorporated. All rights reserved. |
| 2 | +// ReactiveUI Association Incorporated licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using Avalonia.Media; |
| 6 | + |
| 7 | +namespace CrissCross.Avalonia.UI.Extensions; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Adds an extension for <see cref="Color"/> that allows manipulation with HSL and HSV color spaces. |
| 11 | +/// </summary> |
| 12 | +public static class ColorExtensions |
| 13 | +{ |
| 14 | + private const float ByteMax = (float)byte.MaxValue; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Creates a <see cref="SolidColorBrush"/> from a <see cref="Color"/>. |
| 18 | + /// </summary> |
| 19 | + /// <param name="color">Input color.</param> |
| 20 | + /// <returns>Brush converted to color.</returns> |
| 21 | + public static SolidColorBrush ToBrush(this Color color) => new(color); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Creates a <see cref="SolidColorBrush"/> from a <see cref="Color"/> with defined brush opacity. |
| 25 | + /// </summary> |
| 26 | + /// <param name="color">Input color.</param> |
| 27 | + /// <param name="opacity">Degree of opacity.</param> |
| 28 | + /// <returns>Brush converted to color with modified opacity.</returns> |
| 29 | + public static SolidColorBrush ToBrush(this Color color, double opacity) => new(color, opacity); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets <see cref="Color"/> luminance based on HSL space. |
| 33 | + /// </summary> |
| 34 | + /// <param name="color">Input color.</param> |
| 35 | + /// <returns>Luminance value.</returns> |
| 36 | + public static double GetLuminance(this Color color) |
| 37 | + { |
| 38 | + var hsl = color.ToHsl(); |
| 39 | + return hsl.L; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Gets <see cref="Color"/> brightness based on HSV space. |
| 44 | + /// </summary> |
| 45 | + /// <param name="color">Input color.</param> |
| 46 | + /// <returns>Brightness value.</returns> |
| 47 | + public static double GetBrightness(this Color color) |
| 48 | + { |
| 49 | + var hsv = color.ToHsv(); |
| 50 | + return hsv.V; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets <see cref="Color"/> hue based on HSV space. |
| 55 | + /// </summary> |
| 56 | + /// <param name="color">Input color.</param> |
| 57 | + /// <returns>Hue value.</returns> |
| 58 | + public static double GetHue(this Color color) |
| 59 | + { |
| 60 | + var hsv = color.ToHsv(); |
| 61 | + return hsv.H; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets <see cref="Color"/> saturation based on HSV space. |
| 66 | + /// </summary> |
| 67 | + /// <param name="color">Input color.</param> |
| 68 | + /// <returns>Saturation value.</returns> |
| 69 | + public static double GetSaturation(this Color color) |
| 70 | + { |
| 71 | + var hsv = color.ToHsv(); |
| 72 | + return hsv.S; |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Allows to change the luminance by a factor based on the HSL color space. |
| 77 | + /// </summary> |
| 78 | + /// <param name="color">Input color.</param> |
| 79 | + /// <param name="factor">The value of the luminance change factor from 100 to -100.</param> |
| 80 | + /// <returns>Updated <see cref="Color"/>.</returns> |
| 81 | + public static Color UpdateLuminance(this Color color, float factor) |
| 82 | + { |
| 83 | + ArgumentOutOfRangeException.ThrowIfGreaterThan(factor, 100f); |
| 84 | + ArgumentOutOfRangeException.ThrowIfLessThan(factor, -100f); |
| 85 | + |
| 86 | + var hsl = color.ToHsl(); |
| 87 | + var newL = Math.Clamp(hsl.L + (factor / 100.0), 0.0, 1.0); |
| 88 | + return HslColor.ToRgb(hsl.H, hsl.S, newL, hsl.A); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Allows to change the saturation by a factor based on the HSL color space. |
| 93 | + /// </summary> |
| 94 | + /// <param name="color">Input color.</param> |
| 95 | + /// <param name="factor">The value of the saturation change factor from 100 to -100.</param> |
| 96 | + /// <returns>Updated <see cref="Color"/>.</returns> |
| 97 | + public static Color UpdateSaturation(this Color color, float factor) |
| 98 | + { |
| 99 | + ArgumentOutOfRangeException.ThrowIfGreaterThan(factor, 100f); |
| 100 | + ArgumentOutOfRangeException.ThrowIfLessThan(factor, -100f); |
| 101 | + |
| 102 | + var hsl = color.ToHsl(); |
| 103 | + var newS = Math.Clamp(hsl.S + (factor / 100.0), 0.0, 1.0); |
| 104 | + return HslColor.ToRgb(hsl.H, newS, hsl.L, hsl.A); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Allows to change the brightness by a factor based on the HSV color space. |
| 109 | + /// </summary> |
| 110 | + /// <param name="color">Input color.</param> |
| 111 | + /// <param name="factor">The value of the brightness change factor from 100 to -100.</param> |
| 112 | + /// <returns>Updated <see cref="Color"/>.</returns> |
| 113 | + public static Color UpdateBrightness(this Color color, float factor) |
| 114 | + { |
| 115 | + ArgumentOutOfRangeException.ThrowIfGreaterThan(factor, 100f); |
| 116 | + ArgumentOutOfRangeException.ThrowIfLessThan(factor, -100f); |
| 117 | + |
| 118 | + var hsv = color.ToHsv(); |
| 119 | + var newV = Math.Clamp(hsv.V + (factor / 100.0), 0.0, 1.0); |
| 120 | + return HsvColor.ToRgb(hsv.H, hsv.S, newV, hsv.A); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Converts a hex string to a Color. |
| 125 | + /// </summary> |
| 126 | + /// <param name="hex">The hex string.</param> |
| 127 | + /// <returns>The color, or null if parsing fails.</returns> |
| 128 | + public static Color? FromHex(string hex) |
| 129 | + { |
| 130 | + if (Color.TryParse(hex, out var color)) |
| 131 | + { |
| 132 | + return color; |
| 133 | + } |
| 134 | + |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Converts a Color to a hex string. |
| 140 | + /// </summary> |
| 141 | + /// <param name="color">The color.</param> |
| 142 | + /// <returns>The hex string representation.</returns> |
| 143 | + public static string ToHex(this Color color) => color.ToString(); |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Converts a Color to a hex string without alpha. |
| 147 | + /// </summary> |
| 148 | + /// <param name="color">The color.</param> |
| 149 | + /// <returns>The hex string representation without alpha.</returns> |
| 150 | + public static string ToHexWithoutAlpha(this Color color) => |
| 151 | + $"#{color.R:X2}{color.G:X2}{color.B:X2}"; |
| 152 | +} |
0 commit comments