Skip to content

Commit

Permalink
Added the possibility to get the contrast grid of a color (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Feb 23, 2024
1 parent 3e874ab commit cf46773
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ColorPicker/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ public static void ChangeTheme(bool reload = false)
ChromaticWheelPage.CheckButton(ChromaticWheelPage.CheckedButton);
HarmoniesPage.CheckButton(HarmoniesPage.SelectedColorBtn);
PalettePage.CheckButton(PalettePage.SelectedColorBtn);
ContrastPage.CheckButton(ContrastPage.RgbBtn);
ContrastPage.InitGrid();
RefreshButton();
}

Expand Down
41 changes: 36 additions & 5 deletions ColorPicker/Pages/ContrastPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
Margin="5"
VerticalAlignment="Center"
d:Background="#ff0000"
CornerRadius="50"
CornerRadius="60"
Cursor="Hand"
MouseLeftButtonUp="ColorBorder_MouseLeftButtonUp" />
<StackPanel
Expand All @@ -198,7 +198,7 @@
Visibility="Collapsed" />
<Border
x:Name="B1"
Width="50"
Width="60"
HorizontalAlignment="Center"
Background="{DynamicResource CardBackground}"
CornerRadius="5"
Expand Down Expand Up @@ -234,7 +234,7 @@
Visibility="Collapsed" />
<Border
x:Name="B2"
Width="50"
Width="60"
HorizontalAlignment="Center"
Background="{DynamicResource CardBackground}"
CornerRadius="5"
Expand Down Expand Up @@ -270,7 +270,7 @@
Visibility="Collapsed" />
<Border
x:Name="B3"
Width="50"
Width="60"
HorizontalAlignment="Center"
Background="{DynamicResource CardBackground}"
CornerRadius="5"
Expand Down Expand Up @@ -306,7 +306,7 @@
Visibility="Collapsed" />
<Border
x:Name="B4"
Width="50"
Width="60"
HorizontalAlignment="Center"
Background="{DynamicResource CardBackground}"
CornerRadius="5"
Expand Down Expand Up @@ -392,5 +392,36 @@
</Button>
</Grid>
</Border>
<Grid x:Name="ContrastGrid" Grid.Row="2">

<Grid.ColumnDefinitions>
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
<ColumnDefinition Width="60" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
</Grid>
</Grid>
</Page>
58 changes: 58 additions & 0 deletions ColorPicker/Pages/ContrastPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using ColorHelper;
using ColorPicker.Classes;
using ColorPicker.Enums;
using ColorPicker.UserControls;
using Synethia;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -356,6 +358,61 @@ internal void InitGrid()
ColorBorder.Background = new SolidColorBrush { Color = color };
ColorBorder.Effect = new DropShadowEffect() { BlurRadius = 15, ShadowDepth = 0, Color = color };

ContrastGrid.Children.Clear();

List<int> lumValues = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ColorInfo.HSL.L];
lumValues.Sort();
int colorIndex = lumValues.IndexOf(ColorInfo.HSL.L);
if (lumValues[colorIndex] - lumValues[colorIndex - 1] > lumValues[colorIndex + 1] - lumValues[colorIndex])
{
lumValues.RemoveAt(colorIndex + 1);
}
else
{
lumValues.RemoveAt(colorIndex - 1);
}

List<HSL> colors = [];
for (int i = 0; i < lumValues.Count; i++)
{
colors.Add(new(ColorInfo.HSL.H, ColorInfo.HSL.S, (byte)lumValues[i]));
}

for (int i = 0; i < colors.Count; i++)
{
TextBlock textBlock = new()
{
Text = colors[i].L.ToString(),
Foreground = Global.GetColorFromResource("Foreground1"),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
FontWeight = FontWeights.Bold
};
Grid.SetColumn(textBlock, 10 - i + 1);
TextBlock textBlock2 = new()
{
Text = colors[i].L.ToString(),
Foreground = Global.GetColorFromResource("Foreground1"),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
FontWeight = FontWeights.Bold
};
Grid.SetRow(textBlock2, 10 - i + 1);
ContrastGrid.Children.Add(textBlock);
ContrastGrid.Children.Add(textBlock2);
}

for (int i = 0; i < colors.Count; i++)
{
for (int j = 0; j < colors.Count; j++)
{
var colorItem = new ColorGridItem(colors[i], colors[j]);
Grid.SetRow(colorItem, 10 - i + 1);
Grid.SetColumn(colorItem, 10 - j + 1);
ContrastGrid.Children.Add(colorItem);
}
}

// Load the bookmark icon
if (!Global.Bookmarks.ColorBookmarks.Contains($"#{ColorInfo.HEX.Value}"))
{
Expand All @@ -365,5 +422,6 @@ internal void InitGrid()
}
BookmarkBtn.Content = "\uF1F8";
BookmarkToolTip.Content = Properties.Resources.RemoveBookmark;

}
}

0 comments on commit cf46773

Please sign in to comment.