001package squidpony.squidgrid;
002
003/**
004 * Used by DetailedMimic to allow different kinds of detail, including differentiating color or map features.
005 *
006 * Created by Tommy Ettinger on 6/9/2016.
007 */
008public interface AestheticDifference {
009    /**
010     * Finds the difference between two int values, which implementations of this interface may treat as colors, as
011     * kinds of map feature, as item placement factors, or various other ways. The difference should be, on average,
012     * about 1.0 for inputs that have a decent range of values (shades of color as well as changes in hue, etc.).
013     * <br>
014     * The original implementation looked something like this, assuming the SquidColorCenter (from the display module,
015     * not present in squidlib-util) was instantiated earlier for better efficiency:
016     * <pre>
017     *     SquidColorCenter scc = new SquidColorCenter();
018     *     Color c1 = scc.get(a), c2 = scc.get(b);
019     *     return ((c1.r - c2.r) * (c1.r - c2.r) + (c1.g - c2.g) * (c1.g - c2.g) + (c1.b - c2.b) * (c1.b - c2.b)) / 65536.0;
020     * </pre>
021     * @param a an int that may be interpreted in different ways by different implementations
022     * @param b another int that may be interpreted in different ways by different implementations
023     * @return the difference between a and b, ideally averaging about 1.0 for most inputs
024     */
025    double difference(int a, int b);
026
027    AestheticDifference rgba8888 = new AestheticDifference() {
028        @Override
029        public double difference(int a, int b) {
030            int aa = a >>> 24,
031                    bb = b >>> 24,
032                    t = aa - bb,
033                    sum = t * t;
034            aa = (a >>> 16) & 0xff;
035            bb = (b >>> 16) & 0xff;
036            t = aa - bb;
037            sum += t * t;
038            aa = (a >>> 8) & 0xff;
039            bb = (b >>> 8) & 0xff;
040            t = aa - bb;
041            return (sum + t * t) / 65536.0;
042        }
043    };
044}