33
44namespace OpenSourceToolkit . Colors
55{
6+ /// <summary>
7+ /// Provides color conversion helpers for common color formats and color spaces.
8+ /// </summary>
69 public static class ColorConverter
710 {
11+ /// <summary>
12+ /// Converts RGB color components to a hexadecimal color string.
13+ /// </summary>
14+ /// <param name="r">The red component from 0 to 255.</param>
15+ /// <param name="g">The green component from 0 to 255.</param>
16+ /// <param name="b">The blue component from 0 to 255.</param>
17+ /// <returns>A hexadecimal color string in the format #RRGGBB.</returns>
818 public static string RgbToHex ( int r , int g , int b )
919 {
1020 return $ "#{ r : X2} { g : X2} { b : X2} ";
1121 }
1222
23+ /// <summary>
24+ /// Converts a hexadecimal color string to RGB color components.
25+ /// </summary>
26+ /// <param name="hex">The hexadecimal color string to convert.</param>
27+ /// <returns>The red, green, and blue components, or zeros when conversion fails.</returns>
1328 public static ( int R , int G , int B ) HexToRgb ( string hex )
1429 {
1530 try
@@ -23,6 +38,13 @@ public static (int R, int G, int B) HexToRgb(string hex)
2338 }
2439 }
2540
41+ /// <summary>
42+ /// Converts RGB color components to HSL color components.
43+ /// </summary>
44+ /// <param name="r">The red component from 0 to 255.</param>
45+ /// <param name="g">The green component from 0 to 255.</param>
46+ /// <param name="b">The blue component from 0 to 255.</param>
47+ /// <returns>The hue in degrees, saturation from 0 to 1, and lightness from 0 to 1.</returns>
2648 public static ( double H , double S , double L ) RgbToHsl ( int r , int g , int b )
2749 {
2850 double rd = r / 255.0 ;
@@ -58,6 +80,13 @@ public static (double H, double S, double L) RgbToHsl(int r, int g, int b)
5880 return ( h * 360.0 , s , l ) ;
5981 }
6082
83+ /// <summary>
84+ /// Converts HSL color components to RGB color components.
85+ /// </summary>
86+ /// <param name="h">The hue in degrees.</param>
87+ /// <param name="s">The saturation from 0 to 1.</param>
88+ /// <param name="l">The lightness from 0 to 1.</param>
89+ /// <returns>The red, green, and blue components.</returns>
6190 public static ( int R , int G , int B ) HslToRgb ( double h , double s , double l )
6291 {
6392 // h: 0-360, s: 0-1, l: 0-1
@@ -90,6 +119,13 @@ private static double Hue2Rgb(double p, double q, double t)
90119 return p ;
91120 }
92121
122+ /// <summary>
123+ /// Converts RGB color components to HSV color components.
124+ /// </summary>
125+ /// <param name="r">The red component from 0 to 255.</param>
126+ /// <param name="g">The green component from 0 to 255.</param>
127+ /// <param name="b">The blue component from 0 to 255.</param>
128+ /// <returns>The hue in degrees, saturation from 0 to 1, and value from 0 to 1.</returns>
93129 public static ( double H , double S , double V ) RgbToHsv ( int r , int g , int b )
94130 {
95131 double rd = r / 255.0 ;
@@ -116,6 +152,13 @@ public static (double H, double S, double V) RgbToHsv(int r, int g, int b)
116152 return ( h * 360.0 , s , v ) ;
117153 }
118154
155+ /// <summary>
156+ /// Converts RGB color components to CMYK color components.
157+ /// </summary>
158+ /// <param name="r">The red component from 0 to 255.</param>
159+ /// <param name="g">The green component from 0 to 255.</param>
160+ /// <param name="b">The blue component from 0 to 255.</param>
161+ /// <returns>The cyan, magenta, yellow, and key components as percentages.</returns>
119162 public static ( int C , int M , int Y , int K ) RgbToCmyk ( int r , int g , int b )
120163 {
121164 double rd = r / 255.0 ;
@@ -132,6 +175,13 @@ public static (int C, int M, int Y, int K) RgbToCmyk(int r, int g, int b)
132175 return ( ( int ) ( c * 100 ) , ( int ) ( m * 100 ) , ( int ) ( y * 100 ) , ( int ) ( k * 100 ) ) ;
133176 }
134177
178+ /// <summary>
179+ /// Converts RGB color components to CIE L*a*b* color components.
180+ /// </summary>
181+ /// <param name="r">The red component from 0 to 255.</param>
182+ /// <param name="g">The green component from 0 to 255.</param>
183+ /// <param name="b">The blue component from 0 to 255.</param>
184+ /// <returns>The lightness, a-axis, and b-axis components.</returns>
135185 public static ( double L , double A , double B ) RgbToLab ( int r , int g , int b )
136186 {
137187 double R = r / 255.0 ;
0 commit comments