Skip to content

Commit 096b98f

Browse files
committed
C# - Support for lightnesses in color names.
1 parent c75cbfb commit 096b98f

File tree

1 file changed

+90
-5
lines changed

1 file changed

+90
-5
lines changed

ColorSetKit/Color.cs

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ namespace ColorSetKit
3434
[MarkupExtensionReturnType( typeof( SolidColorBrush ) )]
3535
public partial class Color: MarkupExtension
3636
{
37+
private struct NameComponents
38+
{
39+
public string Name;
40+
public double? Lightness;
41+
public string Variant;
42+
}
43+
3744
public Color( string name ): this( name, false, null )
3845
{}
3946

@@ -50,19 +57,97 @@ public Color( string name, bool variant, SolidColorBrush fallback )
5057
this.Fallback = fallback;
5158
}
5259

60+
private NameComponents ComponentsForColorName( string name )
61+
{
62+
int index = name.LastIndexOf( '.' );
63+
64+
if( index < 0 )
65+
{
66+
return new NameComponents { Name = name, Lightness = null, Variant = null };
67+
}
68+
69+
string s1 = name.Substring( 0, index );
70+
string s2 = name.Substring( index + 1 );
71+
72+
if( int.TryParse( s2, out int i ) )
73+
{
74+
return new NameComponents { Name = s1, Lightness = i / 100.0, Variant = null };
75+
}
76+
77+
return new NameComponents { Name = s1, Lightness = null, Variant = s2 };
78+
}
79+
5380
public override object ProvideValue( IServiceProvider provider )
5481
{
55-
if( ColorSet.Shared.ColorNamed( this.Name ) is ColorPair pair )
82+
NameComponents components = this.ComponentsForColorName( this.Name );
83+
84+
if( !( ColorSet.Shared.ColorNamed( this.Name ) is ColorPair pair ) )
5685
{
57-
if( this.Variant && pair.Variant != null )
86+
return this.Fallback;
87+
}
88+
89+
ColorExtension.HSLComponents hsl = pair.Color?.Color.GetHSL() ?? new ColorExtension.HSLComponents { Hue = 0, Saturation = 0, Lightness = 0, Alpha = 0 };
90+
double? lightness = components.Lightness;
91+
92+
if( components.Variant is string v )
93+
{
94+
foreach( LightnessPair lp in pair.Lightnesses )
95+
{
96+
if( v == lp.Lightness1.Name )
97+
{
98+
lightness = lp.Lightness1.Lightness;
99+
100+
break;
101+
}
102+
103+
if( v == lp.Lightness2.Name )
104+
{
105+
lightness = lp.Lightness2.Lightness;
106+
107+
break;
108+
}
109+
}
110+
}
111+
112+
if( lightness.HasValue && lightness.Value is double l && Math.Abs( hsl.Lightness - l ) > 0.001 )
113+
{
114+
if( this.Variant )
58115
{
59-
return pair.Variant;
116+
bool found = false;
117+
118+
foreach( LightnessPair lp in pair.Lightnesses )
119+
{
120+
if( Math.Abs( lp.Lightness1.Lightness - l ) < 0.001 )
121+
{
122+
l = lp.Lightness2.Lightness;
123+
found = true;
124+
125+
break;
126+
}
127+
else if( Math.Abs( lp.Lightness1.Lightness - l ) < 0.001 )
128+
{
129+
l = lp.Lightness1.Lightness;
130+
found = true;
131+
132+
break;
133+
}
134+
}
135+
136+
if( found == false )
137+
{
138+
l = 1.0 - l;
139+
}
60140
}
141+
142+
return ( pair.Color == null ) ? null : new SolidColorBrush( pair.Color.Color.ByChangingLightness( l ) );
143+
}
61144

62-
return pair.Color ?? this.Fallback;
145+
if( this.Variant && pair.Variant is SolidColorBrush variant )
146+
{
147+
return variant;
63148
}
64149

65-
return this.Fallback;
150+
return pair.Color;
66151
}
67152

68153
[ConstructorArgument( "name" )]

0 commit comments

Comments
 (0)