Skip to content

Commit

Permalink
fix exception in palette code
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Jan 24, 2025
1 parent 024bb0b commit 9d60a53
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
24 changes: 16 additions & 8 deletions SkinManagerMod/Items/ShopPaintCanStocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SkinManagerMod.Items
{
public class ShopPaintCanStocker : MonoBehaviour
{
private const int NUM_THEMES_TO_STOCK = 10;
private const int NUM_THEMES_TO_STOCK = 15;

public Shop Shop;
public CashRegisterWithModules CashRegister;
Expand Down Expand Up @@ -44,12 +44,20 @@ public void OnEnable()

for (int i = 0; i < nToStock; i++)
{
var module = CreateModuleForTheme(themes[i]);
_injectedModules.Add(module);

postInjectionModules[originalModuleCount + i] = module;

Main.LogVerbose($"Add shop module for theme {themes[i].name} to {gameObject.name}");
try
{
var module = CreateModuleForTheme(themes[i]);
_injectedModules.Add(module);

postInjectionModules[originalModuleCount + i] = module;

Main.LogVerbose($"Add shop module for theme {themes[i].name} to {gameObject.name}");
}
catch (Exception e)
{
Main.Error($"Failed to create shop module for theme {themes[i].name}: {e.Message}\n{e.StackTrace}");
return;
}
}

Shop.scanItemResourceModules = postInjectionModules;
Expand All @@ -64,7 +72,7 @@ public void OnDisable()

foreach (var module in _injectedModules)
{
Destroy(module.gameObject);
if (module) Destroy(module.gameObject);
}
}

Expand Down
41 changes: 24 additions & 17 deletions SkinManagerMod/Palette/ColorCutQuantizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ namespace OokiiTsuki.Palette
{
public class ColorCutQuantizer
{
private static float[] mTempHsl = new float[3];
private const float BLACK_MAX_LIGHTNESS = 0.05f;
private const float WHITE_MIN_LIGHTNESS = 0.95f;
private const int COMPONENT_RED = -3;
private const int COMPONENT_GREEN = -2;
private const int COMPONENT_BLUE = -1;
private static int[] colors;
private static Dictionary<int, int> mColorPopulations;
private int[] colors;
private Dictionary<int, int> mColorPopulations;
public List<Swatch> QuantizedColors { get; private set; }


Expand Down Expand Up @@ -85,9 +84,11 @@ private List<Swatch> QuantizePixels(int maxColorIndex, int maxColors)
{
// Create the sorted set which is sorted by volume descending. This means we always
// split the largest box in the queue
SortedSet<Vbox> vboxes = new SortedSet<Vbox>();
// To start, add a box which contains all of the colors
vboxes.Add(new Vbox(0, maxColorIndex));
SortedSet<Vbox> vboxes = new SortedSet<Vbox>
{
// To start, add a box which contains all of the colors
new Vbox(colors, mColorPopulations, 0, maxColorIndex)
};
// Now go through the boxes, splitting them until we have reached maxColors or there are no
// more boxes to split
vboxes = SplitBoxes(vboxes, maxColors);
Expand Down Expand Up @@ -140,13 +141,19 @@ private List<Swatch> GenerateAverageColors(SortedSet<Vbox> vboxes)
/// <summary>Represents a tightly fitting box around a color space.</summary>
private class Vbox : IComparable<Vbox>
{
private readonly int[] _colors;
private readonly Dictionary<int, int> _populations;

private int lowerIndex;
private int upperIndex;
private int minRed, maxRed;
private int minGreen, maxGreen;
private int minBlue, maxBlue;
public Vbox(int lowerIndex, int upperIndex)
public Vbox(int[] colors, Dictionary<int, int> populations, int lowerIndex, int upperIndex)
{
_colors = colors;
_populations = populations;

this.lowerIndex = lowerIndex;
this.upperIndex = upperIndex;
FitBox();
Expand Down Expand Up @@ -176,7 +183,7 @@ void FitBox()
maxRed = maxGreen = maxBlue = 0x0;
for (int i = lowerIndex; i <= upperIndex; i++)
{
int color = colors[i];
int color = _colors[i];
int r = color.Red();
int g = color.Green();
int b = color.Blue();
Expand Down Expand Up @@ -217,7 +224,7 @@ public Vbox SplitBox()
}
// find median along the longest dimension
int splitPoint = FindSplitPoint();
Vbox newBox = new Vbox(splitPoint + 1, upperIndex);
Vbox newBox = new Vbox(_colors, _populations, splitPoint + 1, upperIndex);
// Now change this box's upperIndex and recompute the color boundaries
upperIndex = splitPoint;
FitBox();
Expand Down Expand Up @@ -256,15 +263,15 @@ int FindSplitPoint()
// We need to sort the colors in this box based on the longest color dimension.
// As we can't use a Comparator to define the sort logic, we modify each color so that
// it's most significant is the desired dimension
ModifySignificantOctet(longestDimension, lowerIndex, upperIndex);
ModifySignificantOctet(_colors, longestDimension, lowerIndex, upperIndex);
// Now sort...
Array.Sort(colors, lowerIndex, upperIndex + 1 - lowerIndex);
Array.Sort(_colors, lowerIndex, upperIndex + 1 - lowerIndex);
// Now revert all of the colors so that they are packed as RGB again
ModifySignificantOctet(longestDimension, lowerIndex, upperIndex);
ModifySignificantOctet(_colors, longestDimension, lowerIndex, upperIndex);
int dimensionMidPoint = MidPoint(longestDimension);
for (int i = lowerIndex; i < upperIndex; i++)
{
int color = colors[i];
int color = _colors[i];
switch (longestDimension)
{
case COMPONENT_RED:
Expand Down Expand Up @@ -299,8 +306,8 @@ public Swatch GetAverageColor()
int totalPopulation = 0;
for (int i = lowerIndex; i <= upperIndex; i++)
{
int color = colors[i];
int colorPopulation = mColorPopulations[color];
int color = _colors[i];
int colorPopulation = _populations[color];
totalPopulation += colorPopulation;
redSum += colorPopulation * color.Red();
greenSum += colorPopulation * color.Green();
Expand Down Expand Up @@ -333,7 +340,7 @@ int MidPoint(int dimension)
///single color component.
///See <see cref="Vbox.FindSplitPoint()"/>
/// </summary>
private static void ModifySignificantOctet(int dimension, int lowIndex, int highIndex)
private static void ModifySignificantOctet(int[] colors, int dimension, int lowIndex, int highIndex)
{
switch (dimension)
{
Expand All @@ -360,7 +367,7 @@ private static void ModifySignificantOctet(int dimension, int lowIndex, int high
}
private static bool ShouldIgnoreColor(int color)
{
mTempHsl = ColorUtils.RGBtoHSL(color.Red(), color.Green(), color.Blue());
float[] mTempHsl = ColorUtils.RGBtoHSL(color.Red(), color.Green(), color.Blue());
return ShouldIgnoreColor(mTempHsl);
}
private static bool ShouldIgnoreColor(Swatch color)
Expand Down

0 comments on commit 9d60a53

Please sign in to comment.