Skip to content

Commit

Permalink
Added the possibility to get monochromatic colors (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Nov 1, 2023
1 parent 7c915bc commit 275f505
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
21 changes: 21 additions & 0 deletions ColorPicker/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,27 @@ public static string GetRandomAiPrompt()
return prompts[random.Next(prompts.Length)];
}

public static Color[] GenerateMonochromaticColors(Color baseColor, int count, int steps)
{
Color[] monochromaticColors = new Color[count];

// Convert the base color to HSL
float h, s, l;
ColorToHSL(baseColor, out h, out s, out l);

// Calculate the step size for adjusting the lightness
float step = (1.0f - l) / steps;

// Generate monochromatic colors with different lightness values
for (int i = 0; i < count; i++)
{
float newL = l + i * step;
monochromaticColors[i] = HSLToColor(h, s, newL);
}

return monochromaticColors;
}

public static Color[] GenerateAnalogousColors(Color baseColor, int count, int angle = 30)
{
// Ensure that the angle is within the valid range (0-360 degrees).
Expand Down
13 changes: 12 additions & 1 deletion ColorPicker/Pages/HarmoniesPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,18 @@
Margin="5"
FontWeight="Bold"
Text="{x:Static lang:Resources.Analogous}" />
<StackPanel Margin="10" x:Name="AnalogousPanel" Orientation="Horizontal" />
<WrapPanel
x:Name="AnalogousPanel"
Margin="10"
Orientation="Horizontal" />
<TextBlock
Margin="5"
FontWeight="Bold"
Text="{x:Static lang:Resources.Monochromatic}" />
<WrapPanel
x:Name="MonochromaticPanel"
Margin="10"
Orientation="Horizontal" />
</StackPanel>
</Grid>
</Page>
35 changes: 34 additions & 1 deletion ColorPicker/Pages/HarmoniesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,43 @@ internal void InitHarmonies()
Content = new ColorInfo(new(analogousColors[i].R, analogousColors[i].G, analogousColors[i].B)).ToString()
},
};
border.MouseLeftButtonUp += ColorBorder_MouseLeftButtonUp;
border.MouseLeftButtonUp += ComplementaryBorder_MouseLeftButtonUp;

AnalogousPanel.Children.Add(border);
}

// Monochromatic
MonochromaticPanel.Children.Clear();
var monoColors = Global.GenerateMonochromaticColors(color, 6, 6);
for (int i = 0; i < monoColors.Length; i++)
{
CornerRadius radius = i == 0 ? new(10, 0, 0, 10) : new(0);
if (i == monoColors.Length - 1) radius = new(0, 10, 10, 0);
Border border = new()
{
Cursor = Cursors.Hand,
Height = 50,
Width = 50,
CornerRadius = radius,
Background = new SolidColorBrush { Color = Color.FromRgb(monoColors[i].R, monoColors[i].G, monoColors[i].B) },
Effect = new DropShadowEffect()
{
BlurRadius = 15,
Opacity = 0.2,
ShadowDepth = 0,
Color = Color.FromRgb(monoColors[i].R, monoColors[i].G, monoColors[i].B)
},
ToolTip = new ToolTip()
{
Background = new SolidColorBrush { Color = Global.GetColorFromResource("Background1") },
Foreground = new SolidColorBrush { Color = Global.GetColorFromResource("Foreground1") },
Content = new ColorInfo(new(monoColors[i].R, monoColors[i].G, monoColors[i].B)).ToString()
},
};
border.MouseLeftButtonUp += ComplementaryBorder_MouseLeftButtonUp;

MonochromaticPanel.Children.Add(border);
}
}

internal ColorInfo ColorInfo { get; set; }
Expand Down

0 comments on commit 275f505

Please sign in to comment.