Skip to content

Commit

Permalink
- Added drag-drop UI indicators for empty color lists
Browse files Browse the repository at this point in the history
- Added range-checking when importing colors
- Tested practical maximum number of custom colors, settled at roughly 16000
  • Loading branch information
ptr-cs committed Sep 26, 2022
1 parent fd9aee9 commit 1a03319
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 37 deletions.
59 changes: 49 additions & 10 deletions ColorSelector/ColorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@ public ColorSelector()

private void CustomColorsLimitDecrement(object? obj)
{
CustomColorsLimit = Math.Clamp(CustomColorsLimit - 1, 1, CustomColorsLimit_MAX);
CustomColorsLimit = Math.Clamp(CustomColorsLimit - 1, 1, (CustomColorsLimited) ? CustomColorsLimit_MAX : CustomColorsLimit_SAFETY_MAX);
}

private void CustomColorsLimitIncrement(object? obj)
{
CustomColorsLimit = Math.Clamp(CustomColorsLimit + 1, 1, CustomColorsLimit_MAX);
CustomColorsLimit = Math.Clamp(CustomColorsLimit + 1, 1, (CustomColorsLimited) ? CustomColorsLimit_MAX : CustomColorsLimit_SAFETY_MAX);
}

private void ColorColumnsDecrement(object? obj)
Expand Down Expand Up @@ -485,10 +485,11 @@ public override void OnApplyTemplate()
public const double DisplayScale_MIN = .5;
public const double DisplayScale_MAX = 2.0;

public const int CustomColorsLimit_DEFAULT = 10;
public const int CustomColorsLimit_DEFAULT = 20;
public const int CustomColorsLimit_MAX = 1000;
public const int CustomColorsLimit_SAFETY_MAX = 16383; // Memory/performance safety hard-limit

public const int ColorColumns_DEFAULT = 5;
public const int ColorColumns_DEFAULT = 10;
public const int ColorColumns_MAX = 100;

public void ToggleMenu(object? obj)
Expand Down Expand Up @@ -516,14 +517,45 @@ private void DeleteCustomColors(object? obj)
CustomColors.Clear();
}

/// <summary>
/// Returns the int.CompareTo() result for checking the count of a collection of colors.
/// If CustomColorsLimited is set to true, CustomColorsLimit is the comparison value;
/// Else, CustomColorsLimit_SAFETY_MAX is the comparison value.
/// </summary>
/// <returns></returns>
private int CustomColorsLimitReached(IEnumerable<Color>? colorCollection = null)
{
var colleciton = (colorCollection == null) ? CustomColors : colorCollection;
if (CustomColorsLimited)
return colleciton.Count().CompareTo(CustomColorsLimit);
else
return colleciton.Count().CompareTo(CustomColorsLimit_SAFETY_MAX);
}

private int CustomColorsLimitReached()
{
if (CustomColorsLimited)
return CustomColors.Count.CompareTo(CustomColorsLimit);
else
return CustomColors.Count.CompareTo(CustomColorsLimit_SAFETY_MAX);
}

private int GetCustomColorsLimit()
{
if (CustomColorsLimited)
return CustomColorsLimit;
else
return CustomColorsLimit_SAFETY_MAX;
}

/// <summary>
/// Saves custom colors up to a certain limit; once limit is reached,
/// colors are replaced in FILO (First In, Last Out) order;
/// </summary>
/// <param name="obj"></param>
private void SaveCustomColor(object? obj)
{
if (CustomColorsLimited && CustomColors.Count >= CustomColorsLimit)
if (CustomColorsLimitReached() >= 0)
CustomColors.RemoveAt(CustomColors.Count - 1);

CustomColors.Insert(0, GetColorFromRawColor());
Expand Down Expand Up @@ -2334,10 +2366,14 @@ private void ImportFromFile(ImportType importType, string filePath = "")
switch (importType)
{
case ImportType.CustomColors:
CustomColors = new ObservableCollection<Color>(collection);
CustomColors.Clear();
for (int i = 0; i < collection.Count() && i < GetCustomColorsLimit(); ++i)
CustomColors.Add(collection.ElementAt(i));
break;
case ImportType.PresetColors:
PresetColors = new ObservableCollection<Color>(collection);
PresetColors.Clear();
for (int i = 0; i < collection.Count() && i < CustomColorsLimit_SAFETY_MAX; ++i)
PresetColors.Add(collection.ElementAt(i));
break;
default:
break;
Expand Down Expand Up @@ -5817,10 +5853,13 @@ private static void CustomColorsLimitedChanged(DependencyObject d, DependencyPro

private static void UpdateCustomColors(ColorSelector selector)
{
selector.CustomColorsLimitReadout = (selector.CustomColorsLimited) ? selector.CustomColorsLimit.ToString() : "Unlimited";
if (selector.CustomColorsLimited && selector.CustomColors.Count >= selector.CustomColorsLimit)
selector.CustomColorsLimitReadout = (selector.CustomColorsLimited) ? selector.CustomColorsLimit.ToString() : "Unlimited*";
if (selector.CustomColorsLimitReached() >= 1)
{
selector.CustomColors = new(selector.CustomColors.Take(selector.CustomColorsLimit));
var items = new List<Color>(selector.CustomColors.Take(selector.CustomColorsLimit));
selector.CustomColors.Clear();
foreach (var item in items)
selector.CustomColors.Add(item);
}
}

Expand Down
Loading

0 comments on commit 1a03319

Please sign in to comment.