From 829818ded47b88d6b92bf798950a3266b8a19e36 Mon Sep 17 00:00:00 2001 From: "Ray J. Greenwell" Date: Mon, 15 Oct 2018 10:18:44 -0700 Subject: [PATCH] Improve Colorization slightly. Colorizations are not cached, and the root color values are recomputed every time. We can share those. Also added an accessor to read the already computed HSV values out of a Colorization. I suppose I could just make it a public field or return the array directly. The rootColor variable is already public. DFU. But now the array will be shared by every instance from the same classRecord, so it seems more important to make sure nothing goes wrong. --- .../threerings/media/image/ColorPository.java | 22 ++++++++++++++++++- .../threerings/media/image/Colorization.java | 22 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/threerings/media/image/ColorPository.java b/core/src/main/java/com/threerings/media/image/ColorPository.java index c600cce3..cdaf0f03 100644 --- a/core/src/main/java/com/threerings/media/image/ColorPository.java +++ b/core/src/main/java/com/threerings/media/image/ColorPository.java @@ -179,6 +179,26 @@ public class ColorPository implements Serializable StringUtil.toString(colors.values().iterator()) + "]"; } + /** + * Compute the HSV values for the root color if not already computed. + * This is weird, and used to avoid recomputing it for every single colorization made from + * every single ColorRecord in this class. Part of the weirdness is because we + * don't serialize these values to ensure backwards compatibility, and so we need to + * make sure they're computed prior to being used. + */ + protected void computeHsv () + { + if (_hsv == null) { + _hsv = Color.RGBtoHSB(source.getRed(), source.getGreen(), source.getBlue(), null); + _fhsv = Colorization.toFixedHSV(_hsv, null); + } + } + + /** The hsv array shared by all Colorizations created from this instance. */ + protected transient float[] _hsv; + /** The fhsv array shared by all Colorizations created from this instance. */ + protected transient int[] _fhsv; + protected transient ColorRecord[] _starters; /** Increase this value when object's serialized state is impacted @@ -227,7 +247,7 @@ public class ColorPository implements Serializable // cclass.range, offsets); // } // return _zation; - return new Colorization(getColorPrint(), cclass.source, cclass.range, offsets); + return new Colorization(getColorPrint(), cclass, offsets); } // from interface Comparable diff --git a/core/src/main/java/com/threerings/media/image/Colorization.java b/core/src/main/java/com/threerings/media/image/Colorization.java index 002ffd72..f5b4a453 100644 --- a/core/src/main/java/com/threerings/media/image/Colorization.java +++ b/core/src/main/java/com/threerings/media/image/Colorization.java @@ -57,6 +57,20 @@ public class Colorization _fhsv = toFixedHSV(_hsv, null); } + /** + * A colorization generated from a ClassRecord. + */ + protected Colorization (int colorizationId, ColorPository.ClassRecord crec, float[] offsets) + { + this.colorizationId = colorizationId; + this.rootColor = crec.source; + this.range = crec.range; + crec.computeHsv(); + _hsv = crec._hsv; + _fhsv = crec._fhsv; + this.offsets = offsets; + } + /** * Returns the root color adjusted by the colorization. */ @@ -65,6 +79,14 @@ public class Colorization return new Color(recolorColor(_hsv)); } + /** + * Get the raw HSV values of the root color. + */ + public float getRootHsv (int index) + { + return _hsv[index]; + } + /** * Adjusts the supplied color by the offsets in this colorization, taking the appropriate * measures for hue (wrapping it around) and saturation and value (clipping).