1- using System ;
2- using System . Drawing ;
1+ using Avalonia . Media ;
32
4- namespace ByteSync . Common . Helpers ;
3+ namespace ByteSync . Helpers ;
54
65public static class ColorUtils
76{
@@ -25,7 +24,7 @@ public static Color FromHex(string hexColor)
2524 Color color ;
2625 if ( bytes . Length == 3 )
2726 {
28- color = Color . FromArgb ( bytes [ 0 ] , bytes [ 1 ] , bytes [ 2 ] ) ;
27+ color = Color . FromArgb ( 255 , bytes [ 0 ] , bytes [ 1 ] , bytes [ 2 ] ) ;
2928 }
3029 else if ( bytes . Length == 4 )
3130 {
@@ -47,17 +46,9 @@ public static string ToHex(Color color)
4746 public static int GetHexVal ( char hex )
4847 {
4948 int val = ( int ) hex ;
50- //For uppercase A-F letters:
51- //return val - (val < 58 ? 48 : 55);
52- //For lowercase a-f letters:
53- //return val - (val < 58 ? 48 : 87);
54- //Or the two combined, but a bit slower:
5549 return val - ( val < 58 ? 48 : ( val < 97 ? 55 : 87 ) ) ;
5650 }
5751
58-
59- // http://csharphelper.com/blog/2016/08/convert-between-rgb-and-hls-color-models-in-c/
60-
6152 public static void RgbToHls ( Color color , out double h , out double l , out double s )
6253 {
6354 RgbToHls ( color . R , color . G , color . B , out h , out l , out s ) ;
@@ -104,19 +95,6 @@ public static void RgbToHls(int r, int g, int b, out double h, out double l, out
10495 }
10596 }
10697
107- public static Color HlsToRgb ( double h , double l , double s )
108- {
109- byte r ;
110- byte g ;
111- byte b ;
112-
113- HlsToRgb ( h , l , s , out r , out g , out b ) ;
114-
115- Color color = Color . FromArgb ( r , g , b ) ;
116-
117- return color ;
118- }
119-
12098 // Convert an HLS value into an RGB value.
12199 public static void HlsToRgb ( double h , double l , double s , out byte r , out byte g , out byte b )
122100 {
@@ -156,69 +134,49 @@ private static double QqhToRgb(double q1, double q2, double hue)
156134 return q1 ;
157135 }
158136
159- // https://stackoverflow.com/questions/3722307/is-there-an-easy-way-to-blend-two-system-drawing-color-values
160- /// <summary>Blends the specified colors together.</summary>
161- /// <param name="color">Color to blend onto the background color.</param>
162- /// <param name="backColor">Color to blend the other color onto.</param>
163- /// <param name="amount">How much of <paramref name="color"/> to keep,
164- /// “on top of” <paramref name="backColor"/>.</param>
165- /// <returns>The blended colors.</returns>
166- public static Color Blend ( Color color , Color backColor , double amount )
137+ public static Color BlendWithTransparency ( Color baseColor , Color overlayColor , double opacity )
167138 {
168- byte r = ( byte ) ( color . R * amount + backColor . R * ( 1 - amount ) ) ;
169- byte g = ( byte ) ( color . G * amount + backColor . G * ( 1 - amount ) ) ;
170- byte b = ( byte ) ( color . B * amount + backColor . B * ( 1 - amount ) ) ;
171- return Color . FromArgb ( r , g , b ) ;
172- }
139+ opacity = Math . Clamp ( opacity , 0 , 1 ) ;
173140
174- /// <summary>
175- /// Creates color with corrected brightness.
176- /// </summary>
177- /// <param name="color">Color to correct.</param>
178- /// <param name="correctionFactor">The brightness correction factor. Must be between -1 and 1.
179- /// Negative values produce darker colors.</param>
180- /// <returns>
181- /// Corrected <see cref="Color"/> structure.
182- /// </returns>
183- public static Color ChangeColorBrightness ( Color color , float correctionFactor )
184- {
185- https : //stackoverflow.com/questions/801406/c-create-a-lighter-darker-color-based-on-a-system-color
186-
187- float red = ( float ) color . R ;
188- float green = ( float ) color . G ;
189- float blue = ( float ) color . B ;
190-
191- if ( correctionFactor < 0 )
192- {
193- correctionFactor = 1 + correctionFactor ;
194- red *= correctionFactor ;
195- green *= correctionFactor ;
196- blue *= correctionFactor ;
197- }
198- else
199- {
200- red = ( 255 - red ) * correctionFactor + red ;
201- green = ( 255 - green ) * correctionFactor + green ;
202- blue = ( 255 - blue ) * correctionFactor + blue ;
203- }
204-
205- return Color . FromArgb ( color . A , ( int ) red , ( int ) green , ( int ) blue ) ;
206- }
141+ // Normalize RGB values between 0 and 1
142+ double baseR = baseColor . R / 255.0 ;
143+ double baseG = baseColor . G / 255.0 ;
144+ double baseB = baseColor . B / 255.0 ;
145+
146+ double overlayR = overlayColor . R / 255.0 ;
147+ double overlayG = overlayColor . G / 255.0 ;
148+ double overlayB = overlayColor . B / 255.0 ;
207149
208- // Testé le 13/12/2022 : https://stackoverflow.com/questions/359612/how-to-convert-rgb-color-to-hsv
209- // Même résultat qu'avec Gimp
150+ // Apply the blending formula with transparency
151+ // For each component: result = base + (overlay - base) * opacity
152+ double resultR = baseR + ( overlayR - baseR ) * opacity ;
153+ double resultG = baseG + ( overlayG - baseG ) * opacity ;
154+ double resultB = baseB + ( overlayB - baseB ) * opacity ;
210155
211- public static void ColorToHSV ( Color color , out double hue , out double saturation , out double value )
156+ // Convert to values 0-255
157+ byte r = ( byte ) ( resultR * 255 ) ;
158+ byte g = ( byte ) ( resultG * 255 ) ;
159+ byte b = ( byte ) ( resultB * 255 ) ;
160+
161+ return Color . FromArgb ( baseColor . A , r , g , b ) ;
162+ }
163+
164+ public static void ColorToHsv ( Color color , out double hue , out double saturation , out double value )
212165 {
213166 int max = Math . Max ( color . R , Math . Max ( color . G , color . B ) ) ;
214167 int min = Math . Min ( color . R , Math . Min ( color . G , color . B ) ) ;
215168
216- hue = color . GetHue ( ) ;
169+ hue = color . ToSystemColor ( ) . GetHue ( ) ;
217170 saturation = ( max == 0 ) ? 0 : 1d - ( 1d * min / max ) ;
218171 value = max / 255d ;
219172 }
220173
221- public static Color ColorFromHSV ( double hue , double saturation , double value )
174+ public static System . Drawing . Color ToSystemColor ( this Color color )
175+ {
176+ return System . Drawing . Color . FromArgb ( color . A , color . R , color . G , color . B ) ;
177+ }
178+
179+ public static Color ColorFromHsv ( double hue , double saturation , double value )
222180 {
223181 int hi = Convert . ToInt32 ( Math . Floor ( hue / 60 ) ) % 6 ;
224182 double f = hue / 60 - Math . Floor ( hue / 60 ) ;
@@ -230,16 +188,16 @@ public static Color ColorFromHSV(double hue, double saturation, double value)
230188 int t = Convert . ToInt32 ( value * ( 1 - ( 1 - f ) * saturation ) ) ;
231189
232190 if ( hi == 0 )
233- return Color . FromArgb ( 255 , v , t , p ) ;
191+ return Color . FromArgb ( 255 , ( byte ) v , ( byte ) t , ( byte ) p ) ;
234192 else if ( hi == 1 )
235- return Color . FromArgb ( 255 , q , v , p ) ;
193+ return Color . FromArgb ( 255 , ( byte ) q , ( byte ) v , ( byte ) p ) ;
236194 else if ( hi == 2 )
237- return Color . FromArgb ( 255 , p , v , t ) ;
195+ return Color . FromArgb ( 255 , ( byte ) p , ( byte ) v , ( byte ) t ) ;
238196 else if ( hi == 3 )
239- return Color . FromArgb ( 255 , p , q , v ) ;
197+ return Color . FromArgb ( 255 , ( byte ) p , ( byte ) q , ( byte ) v ) ;
240198 else if ( hi == 4 )
241- return Color . FromArgb ( 255 , t , p , v ) ;
199+ return Color . FromArgb ( 255 , ( byte ) t , ( byte ) p , ( byte ) v ) ;
242200 else
243- return Color . FromArgb ( 255 , v , p , q ) ;
201+ return Color . FromArgb ( 255 , ( byte ) v , ( byte ) p , ( byte ) q ) ;
244202 }
245203}
0 commit comments