@@ -137,22 +137,78 @@ private static void WriteNonGenericStaticProperties(IndentedTextWriter writer, b
137137 writer . WriteLine ( $ "static Type IColor.ChannelType => typeof({ typeName } );") ;
138138 }
139139
140+ private static void WriteBlackWhiteStaticProperties ( IndentedTextWriter writer , bool hasRed , bool hasGreen , bool hasBlue , bool hasAlpha , string colorTypeName , string channelTypeName )
141+ {
142+ string minValue = $ "NumericConversion.GetMinimumValueSafe<{ channelTypeName } >()";
143+ string maxValue = $ "NumericConversion.GetMaximumValueSafe<{ channelTypeName } >()";
144+
145+ //Black
146+ {
147+ writer . Write ( $ "public static { colorTypeName } <{ channelTypeName } > Black => new(") ;
148+ int rgbCount = CountRGB ( hasRed , hasGreen , hasBlue ) ;
149+ for ( int i = 0 ; i < rgbCount ; i ++ )
150+ {
151+ if ( i != 0 )
152+ {
153+ writer . Write ( ", " ) ;
154+ }
155+ writer . Write ( minValue ) ;
156+ }
157+ if ( hasAlpha )
158+ {
159+ if ( rgbCount != 0 )
160+ {
161+ writer . Write ( ", " ) ;
162+ }
163+ writer . Write ( maxValue ) ;
164+ }
165+ writer . WriteLine ( ");" ) ;
166+ }
167+
168+ //White
169+ if ( hasRed || hasGreen || hasBlue )
170+ {
171+ writer . Write ( $ "public static { colorTypeName } <{ channelTypeName } > White => new(") ;
172+ int rgbaCount = CountRGBA ( hasRed , hasGreen , hasBlue , hasAlpha ) ;
173+ for ( int i = 0 ; i < rgbaCount ; i ++ )
174+ {
175+ if ( i != 0 )
176+ {
177+ writer . Write ( ", " ) ;
178+ }
179+ writer . Write ( maxValue ) ;
180+ }
181+ writer . WriteLine ( ");" ) ;
182+ }
183+ else
184+ {
185+ writer . WriteLine ( $ "static { colorTypeName } <{ channelTypeName } > IColor<{ colorTypeName } <{ channelTypeName } >, { channelTypeName } >.White => throw new NotSupportedException();") ;
186+ }
187+
188+ static int CountRGB ( bool hasRed , bool hasGreen , bool hasBlue )
189+ {
190+ return ( hasRed ? 1 : 0 ) + ( hasGreen ? 1 : 0 ) + ( hasBlue ? 1 : 0 ) ;
191+ }
192+
193+ static int CountRGBA ( bool hasRed , bool hasGreen , bool hasBlue , bool hasAlpha )
194+ {
195+ return CountRGB ( hasRed , hasGreen , hasBlue ) + ( hasAlpha ? 1 : 0 ) ;
196+ }
197+ }
198+
140199 private static void WriteGenericColor ( IndentedTextWriter writer , string name , bool hasRed , bool hasGreen , bool hasBlue , bool hasAlpha )
141200 {
142201 const string typeName = "T" ;
143- const string minValue = $ "NumericConversion.GetMinimumValueSafe<T>()";
144- const string maxValue = $ "NumericConversion.GetMaximumValueSafe<T>()";
202+ const string minValue = "NumericConversion.GetMinimumValueSafe<T>()" ;
203+ const string maxValue = "NumericConversion.GetMaximumValueSafe<T>()" ;
145204
146205 writer . WriteGeneratedCodeWarning ( ) ;
147206 writer . WriteLineNoTabs ( ) ;
148207
149208 writer . WriteFileScopedNamespace ( OutputNamespace ) ;
150209 writer . WriteLineNoTabs ( ) ;
151210
152- string constraints = hasRed && hasGreen && hasBlue && hasAlpha
153- ? "unmanaged"
154- : "unmanaged, INumberBase<T>, IMinMaxValue<T>" ;
155- writer . WriteLine ( $ "public partial struct { name } <T> : IColor<{ typeName } > where T : { constraints } ") ;
211+ writer . WriteLine ( $ "public partial struct { name } <T> : IColor<{ name } <T>, T> where T : unmanaged, INumberBase<T>, IMinMaxValue<T>") ;
156212 using ( new CurlyBrackets ( writer ) )
157213 {
158214 WriteProperty ( writer , hasRed , typeName , 'R' , minValue ) ;
@@ -172,6 +228,8 @@ private static void WriteGenericColor(IndentedTextWriter writer, string name, bo
172228 writer . WriteLineNoTabs ( ) ;
173229 WriteNonGenericStaticProperties ( writer , hasRed , hasGreen , hasBlue , hasAlpha , true , typeName ) ;
174230 writer . WriteLineNoTabs ( ) ;
231+ WriteBlackWhiteStaticProperties ( writer , hasRed , hasGreen , hasBlue , hasAlpha , name , typeName ) ;
232+ writer . WriteLineNoTabs ( ) ;
175233 WriteToString ( writer , hasRed , hasGreen , hasBlue , hasAlpha ) ;
176234 }
177235 }
0 commit comments