Ticket #18961: 18961.patch

File 18961.patch, 29.5 KB (added by simon04, 4 years ago)
  • src/org/openstreetmap/josm/gui/SelectionManager.java

    commit 2db67d1fcdaba34dbe91b7f17d0a419c7f0283d4
    Author: Simon Legner <Simon.Legner@gmail.com>
    Date:   2020-03-21 15:21:39 +0100
    
        fix #18961 - ColorHelper: harmonize color functions
    
    diff --git a/src/org/openstreetmap/josm/gui/SelectionManager.java b/src/org/openstreetmap/josm/gui/SelectionManager.java
    index 8a82c1b85..a6022b1f7 100644
    a b  
    2525import org.openstreetmap.josm.data.osm.Way;
    2626import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors;
    2727import org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable;
    28 import org.openstreetmap.josm.tools.Utils;
     28import org.openstreetmap.josm.tools.ColorHelper;
    2929
    3030/**
    3131 * Manages the selection of a rectangle or a lasso loop. Listening to left and right mouse button
     
    8282        public void paint(Graphics2D g, MapView mv, Bounds bbox) {
    8383            if (mousePos == null || mousePosStart == null || mousePos == mousePosStart)
    8484                return;
    85             Color color = Utils.complement(PaintColors.getBackgroundColor());
     85            Color color = ColorHelper.complement(PaintColors.getBackgroundColor());
    8686            g.setColor(color);
    8787            if (lassoMode) {
    8888                g.drawPolygon(lasso);
  • src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    diff --git a/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java b/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java
    index 57591e752..e08739c92 100644
    a b  
    7474import org.openstreetmap.josm.gui.widgets.JosmTextArea;
    7575import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    7676import org.openstreetmap.josm.gui.widgets.ScrollableTable;
     77import org.openstreetmap.josm.tools.ColorHelper;
    7778import org.openstreetmap.josm.tools.GBC;
    7879import org.openstreetmap.josm.tools.ImageOverlay;
    7980import org.openstreetmap.josm.tools.ImageProvider;
    private JPanel buildInfoPanel(StyleSource s) {  
    611612                text.append(tableRow(tr("Icon:"), s.icon));
    612613            }
    613614            if (s.getBackgroundColorOverride() != null) {
    614                 text.append(tableRow(tr("Background:"), Utils.toString(s.getBackgroundColorOverride())));
     615                text.append(tableRow(tr("Background:"), ColorHelper.color2html(s.getBackgroundColorOverride())));
    615616            }
    616617            text.append(tableRow(tr("Style is currently active?"), s.active ? tr("Yes") : tr("No")))
    617618                .append("</table>");
  • src/org/openstreetmap/josm/gui/mappaint/Cascade.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/Cascade.java b/src/org/openstreetmap/josm/gui/mappaint/Cascade.java
    index ea74f1a08..922f771b5 100644
    a b  
    1414import org.openstreetmap.josm.tools.ColorHelper;
    1515import org.openstreetmap.josm.tools.GenericParser;
    1616import org.openstreetmap.josm.tools.Logging;
    17 import org.openstreetmap.josm.tools.Utils;
    1817
    1918/**
    2019 * Simple map of properties with dynamic typing.
    public String toString() {  
    250249            if (val instanceof float[]) {
    251250                sb.append(Arrays.toString((float[]) val));
    252251            } else if (val instanceof Color) {
    253                 sb.append(Utils.toString((Color) val));
     252                sb.append(ColorHelper.color2html((Color) val));
    254253            } else if (val != null) {
    255254                sb.append(val);
    256255            }
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java b/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java
    index 891f59d98..79cbcb539 100644
    a b public static String color2html(Color c) { // NO_UCD (unused code)  
    270270     * @see java.awt.Color#getRed()
    271271     */
    272272    public static float red(Color c) { // NO_UCD (unused code)
    273         return Utils.colorInt2float(c.getRed());
     273        return ColorHelper.int2float(c.getRed());
    274274    }
    275275
    276276    /**
    public static float red(Color c) { // NO_UCD (unused code)  
    280280     * @see java.awt.Color#getGreen()
    281281     */
    282282    public static float green(Color c) { // NO_UCD (unused code)
    283         return Utils.colorInt2float(c.getGreen());
     283        return ColorHelper.int2float(c.getGreen());
    284284    }
    285285
    286286    /**
    public static float green(Color c) { // NO_UCD (unused code)  
    290290     * @see java.awt.Color#getBlue()
    291291     */
    292292    public static float blue(Color c) { // NO_UCD (unused code)
    293         return Utils.colorInt2float(c.getBlue());
     293        return ColorHelper.int2float(c.getBlue());
    294294    }
    295295
    296296    /**
    public static float blue(Color c) { // NO_UCD (unused code)  
    300300     * @see java.awt.Color#getAlpha()
    301301     */
    302302    public static float alpha(Color c) { // NO_UCD (unused code)
    303         return Utils.colorInt2float(c.getAlpha());
     303        return ColorHelper.int2float(c.getAlpha());
    304304    }
    305305
    306306    /**
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/AreaElement.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/AreaElement.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/AreaElement.java
    index 37ab21944..bf3dac9bf 100644
    a b  
    1717import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    1818import org.openstreetmap.josm.spi.preferences.Config;
    1919import org.openstreetmap.josm.tools.CheckParameterUtil;
     20import org.openstreetmap.josm.tools.ColorHelper;
    2021import org.openstreetmap.josm.tools.HiDPISupport;
    2122import org.openstreetmap.josm.tools.Utils;
    2223import org.openstreetmap.josm.tools.bugreport.BugReport;
    public static AreaElement create(final Environment env) {  
    9596            }
    9697
    9798            fillImage.alpha = Utils.clamp(Config.getPref().getInt("mappaint.fill-image-alpha", 255), 0, 255);
    98             Integer pAlpha = Utils.colorFloat2int(c.get(FILL_OPACITY, null, float.class));
     99            Integer pAlpha = ColorHelper.float2int(c.get(FILL_OPACITY, null, float.class));
    99100            if (pAlpha != null) {
    100101                fillImage.alpha = pAlpha;
    101102            }
    102103        } else {
    103104            color = c.get(FILL_COLOR, null, Color.class);
    104105            if (color != null) {
    105                 float defaultOpacity = Utils.colorInt2float(DEFAULT_FILL_ALPHA.get());
     106                float defaultOpacity = ColorHelper.int2float(DEFAULT_FILL_ALPHA.get());
    106107                float opacity = c.get(FILL_OPACITY, defaultOpacity, Float.class);
    107                 color = Utils.alphaMultiply(color, opacity);
     108                color = ColorHelper.alphaMultiply(color, opacity);
    108109            }
    109110        }
    110111
    public int hashCode() {  
    158159
    159160    @Override
    160161    public String toString() {
    161         return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +
     162        return "AreaElemStyle{" + super.toString() + "color=" + ColorHelper.color2html(color) +
    162163                " fillImage=[" + fillImage + "] extent=[" + extent + "] extentThreshold=[" + extentThreshold + "]}";
    163164    }
    164165}
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/LineElement.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/LineElement.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/LineElement.java
    index 4afc24b0a..d035fc3f2 100644
    a b  
    1919import org.openstreetmap.josm.gui.mappaint.Keyword;
    2020import org.openstreetmap.josm.gui.mappaint.MultiCascade;
    2121import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat;
     22import org.openstreetmap.josm.tools.ColorHelper;
    2223import org.openstreetmap.josm.tools.Logging;
    23 import org.openstreetmap.josm.tools.Utils;
    2424
    2525/**
    2626 * This is the style definition for a simple line.
    public int hashCode() {  
    234234    @Override
    235235    public String toString() {
    236236        return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() +
    237             " realWidth=" + realWidth + " color=" + Utils.toString(color) +
     237            " realWidth=" + realWidth + " color=" + ColorHelper.color2html(color) +
    238238            " dashed=" + Arrays.toString(line.getDashArray()) +
    239239            (line.getDashPhase() == 0 ? "" : " dashesOffses=" + line.getDashPhase()) +
    240             " dashedColor=" + Utils.toString(dashesBackground) +
     240            " dashedColor=" + ColorHelper.color2html(dashesBackground) +
    241241            " linejoin=" + linejoinToString(line.getLineJoin()) +
    242242            " linecap=" + linecapToString(line.getEndCap()) +
    243243            (offset == 0 ? "" : " offset=" + offset) +
    private static LineElement createImpl(Environment env, LineType type) {  
    333333            color = PaintColors.UNTAGGED.get();
    334334        }
    335335
    336         Integer pAlpha = Utils.colorFloat2int(c.get(type.prefix + OPACITY, null, Float.class));
     336        Integer pAlpha = ColorHelper.float2int(c.get(type.prefix + OPACITY, null, Float.class));
    337337        if (pAlpha != null) {
    338338            alpha = pAlpha;
    339339        }
    private static LineElement createImpl(Environment env, LineType type) {  
    362362        }
    363363        Color dashesBackground = c.get(type.prefix + DASHES_BACKGROUND_COLOR, null, Color.class);
    364364        if (dashesBackground != null) {
    365             pAlpha = Utils.colorFloat2int(c.get(type.prefix + DASHES_BACKGROUND_OPACITY, null, Float.class));
     365            pAlpha = ColorHelper.float2int(c.get(type.prefix + DASHES_BACKGROUND_OPACITY, null, Float.class));
    366366            if (pAlpha != null) {
    367367                alpha = pAlpha;
    368368            }
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java
    index 238254cb6..f447f0177 100644
    a b  
    2121import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProvider;
    2222import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProviderResult;
    2323import org.openstreetmap.josm.gui.util.GuiHelper;
     24import org.openstreetmap.josm.tools.ColorHelper;
    2425import org.openstreetmap.josm.tools.ImageProvider;
    2526import org.openstreetmap.josm.tools.ImageResource;
    2627import org.openstreetmap.josm.tools.Logging;
    27 import org.openstreetmap.josm.tools.Utils;
    2828
    2929/**
    3030 * An image that will be displayed on the map.
    public int getHeight() {  
    214214     * @return The value in range 0..1
    215215     */
    216216    public float getAlphaFloat() {
    217         return Utils.colorInt2float(alpha);
     217        return ColorHelper.int2float(alpha);
    218218    }
    219219
    220220    /**
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/NodeElement.java
    index a0a184537..8a3bbd897 100644
    a b  
    2323import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.SimpleBoxProvider;
    2424import org.openstreetmap.josm.spi.preferences.Config;
    2525import org.openstreetmap.josm.tools.CheckParameterUtil;
     26import org.openstreetmap.josm.tools.ColorHelper;
    2627import org.openstreetmap.josm.tools.Logging;
    2728import org.openstreetmap.josm.tools.RotationAngle;
    2829import org.openstreetmap.josm.tools.Utils;
    public static MapImage createIcon(final Environment env, final String... keys) {  
    167168        mapImage.offsetY = Math.round(offsetYF);
    168169
    169170        mapImage.alpha = Utils.clamp(Config.getPref().getInt("mappaint.icon-image-alpha", 255), 0, 255);
    170         Integer pAlpha = Utils.colorFloat2int(c.get(keys[ICON_OPACITY_IDX], null, float.class));
     171        Integer pAlpha = ColorHelper.float2int(c.get(keys[ICON_OPACITY_IDX], null, float.class));
    171172        if (pAlpha != null) {
    172173            mapImage.alpha = pAlpha;
    173174        }
    private static Symbol createSymbol(Environment env) {  
    212213
    213214        Stroke stroke = null;
    214215        if (strokeColor != null && strokeWidth != null) {
    215             Integer strokeAlpha = Utils.colorFloat2int(c.get("symbol-stroke-opacity", null, Float.class));
     216            Integer strokeAlpha = ColorHelper.float2int(c.get("symbol-stroke-opacity", null, Float.class));
    216217            if (strokeAlpha != null) {
    217218                strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
    218219                        strokeColor.getBlue(), strokeAlpha);
    private static Symbol createSymbol(Environment env) {  
    226227        }
    227228
    228229        if (fillColor != null) {
    229             Integer fillAlpha = Utils.colorFloat2int(c.get("symbol-fill-opacity", null, Float.class));
     230            Integer fillAlpha = ColorHelper.float2int(c.get("symbol-fill-opacity", null, Float.class));
    230231            if (fillAlpha != null) {
    231232                fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
    232233                        fillColor.getBlue(), fillAlpha);
  • src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java

    diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/TextLabel.java
    index 829385f8a..befca5c33 100644
    a b  
    1717import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.StaticLabelCompositionStrategy;
    1818import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.TagLookupCompositionStrategy;
    1919import org.openstreetmap.josm.tools.CheckParameterUtil;
    20 import org.openstreetmap.josm.tools.Utils;
     20import org.openstreetmap.josm.tools.ColorHelper;
    2121
    2222/**
    2323 * Represents the rendering style for a textual label placed somewhere on the map.
    public static TextLabel create(Environment env, Color defaultTextColor, boolean  
    139139
    140140        Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class);
    141141        float alpha = c.get(TEXT_OPACITY, 1f, Float.class);
    142         color = Utils.alphaMultiply(color, alpha);
     142        color = ColorHelper.alphaMultiply(color, alpha);
    143143
    144144        Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class);
    145145        if (haloRadius != null && haloRadius <= 0) {
    public static TextLabel create(Environment env, Color defaultTextColor, boolean  
    147147        }
    148148        Color haloColor = null;
    149149        if (haloRadius != null) {
    150             haloColor = c.get(TEXT_HALO_COLOR, Utils.complement(color), Color.class);
     150            haloColor = c.get(TEXT_HALO_COLOR, ColorHelper.complement(color), Color.class);
    151151            float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class);
    152             haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor);
     152            haloColor = ColorHelper.alphaMultiply(haloColor, haloAlphaFactor);
    153153        }
    154154
    155155        return new TextLabel(strategy, font, color, haloRadius, haloColor);
    protected String toStringImpl() {  
    198198        StringBuilder sb = new StringBuilder(96);
    199199        sb.append("labelCompositionStrategy=").append(labelCompositionStrategy)
    200200          .append(" font=").append(font)
    201           .append(" color=").append(Utils.toString(color));
     201          .append(" color=").append(ColorHelper.color2html(color));
    202202        if (haloRadius != null) {
    203203            sb.append(" haloRadius=").append(haloRadius)
    204204              .append(" haloColor=").append(haloColor);
  • src/org/openstreetmap/josm/tools/ColorHelper.java

    diff --git a/src/org/openstreetmap/josm/tools/ColorHelper.java b/src/org/openstreetmap/josm/tools/ColorHelper.java
    index ac824a41f..eb654238c 100644
    a b  
    22package org.openstreetmap.josm.tools;
    33
    44import java.awt.Color;
    5 import java.util.Locale;
    65
    76/**
    87 * Helper to convert from color to HTML string and back.
    public static Color html2color(String html) {  
    3837        }
    3938    }
    4039
    41     private static String int2hex(int i) {
    42         String s = Integer.toHexString(i / 16) + Integer.toHexString(i % 16);
    43         return s.toUpperCase(Locale.ENGLISH);
    44     }
    45 
    4640    /**
    4741     * Returns the HTML color code (6 or 8 digit).
    4842     * @param col The color to convert
    public static String color2html(Color col) {  
    5448
    5549    /**
    5650     * Returns the HTML color code (6 or 8 digit).
    57      * @param col The color to convert
     51     * @param color The color to convert
    5852     * @param withAlpha if {@code true} and alpha value &lt; 255, return 8-digit color code, else always 6-digit
    5953     * @return the HTML color code (6 or 8 digit)
    6054     * @since 6655
    6155     */
    62     public static String color2html(Color col, boolean withAlpha) {
    63         if (col == null)
     56    public static String color2html(Color color, boolean withAlpha) {
     57        if (color == null)
    6458            return null;
    65         String code = '#'+int2hex(col.getRed())+int2hex(col.getGreen())+int2hex(col.getBlue());
    66         if (withAlpha) {
    67             int alpha = col.getAlpha();
    68             if (alpha < 255) {
    69                 code += int2hex(alpha);
    70             }
    71         }
    72         return code;
     59        int alpha = color.getAlpha();
     60        return withAlpha && alpha != 255
     61                ? String.format("#%06X%02X", color.getRGB() & 0x00ffffff, alpha)
     62                : String.format("#%06X", color.getRGB() & 0x00ffffff);
    7363    }
    7464
    7565    /**
    public static Color getForegroundColor(Color bg) {  
    8575              (bg.getRed()*0.299 + bg.getGreen()*0.587 + bg.getBlue()*0.114) > 186 ?
    8676                  Color.BLACK : Color.WHITE;
    8777    }
     78
     79    /**
     80     * convert float range 0 &lt;= x &lt;= 1 to integer range 0..255
     81     * when dealing with colors and color alpha value
     82     * @param val float value between 0 and 1
     83     * @return null if val is null, the corresponding int if val is in the
     84     *         range 0...1. If val is outside that range, return 255
     85     */
     86    public static Integer float2int(Float val) {
     87        if (val == null)
     88            return null;
     89        if (val < 0 || val > 1)
     90            return 255;
     91        return (int) (255f * val + 0.5f);
     92    }
     93
     94    /**
     95     * convert integer range 0..255 to float range 0 &lt;= x &lt;= 1
     96     * when dealing with colors and color alpha value
     97     * @param val integer value
     98     * @return corresponding float value in range 0 &lt;= x &lt;= 1
     99     */
     100    public static Float int2float(Integer val) {
     101        if (val == null)
     102            return null;
     103        if (val < 0 || val > 255)
     104            return 1f;
     105        return ((float) val) / 255f;
     106    }
     107
     108    /**
     109     * Multiply the alpha value of the given color with the factor. The alpha value is clamped to 0..255
     110     * @param color The color
     111     * @param alphaFactor The factor to multiply alpha with.
     112     * @return The new color.
     113     * @since 11692
     114     */
     115    public static Color alphaMultiply(Color color, float alphaFactor) {
     116        int alpha = float2int(int2float(color.getAlpha()) * alphaFactor);
     117        alpha = Utils.clamp(alpha, 0, 255);
     118        return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
     119    }
     120
     121    /**
     122     * Returns the complementary color of {@code clr}.
     123     * @param clr the color to complement
     124     * @return the complementary color of {@code clr}
     125     */
     126    public static Color complement(Color clr) {
     127        return new Color(255 - clr.getRed(), 255 - clr.getGreen(), 255 - clr.getBlue(), clr.getAlpha());
     128    }
    88129}
  • src/org/openstreetmap/josm/tools/Utils.java

    diff --git a/src/org/openstreetmap/josm/tools/Utils.java b/src/org/openstreetmap/josm/tools/Utils.java
    index aa15d7214..84fbef5cd 100644
    a b  
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66import static org.openstreetmap.josm.tools.I18n.trn;
    77
    8 import java.awt.Color;
    98import java.awt.Font;
    109import java.awt.font.FontRenderContext;
    1110import java.awt.font.GlyphVector;
    public static String joinAsHtmlUnorderedList(Iterable<?> values) {  
    256255        return StreamUtils.toStream(values).map(Object::toString).collect(StreamUtils.toHtmlList());
    257256    }
    258257
    259     /**
    260      * convert Color to String
    261      * (Color.toString() omits alpha value)
    262      * @param c the color
    263      * @return the String representation, including alpha
    264      */
    265     public static String toString(Color c) {
    266         if (c == null)
    267             return "null";
    268         if (c.getAlpha() == 255)
    269             return String.format("#%06x", c.getRGB() & 0x00ffffff);
    270         else
    271             return String.format("#%06x(alpha=%d)", c.getRGB() & 0x00ffffff, c.getAlpha());
    272     }
    273 
    274     /**
    275      * convert float range 0 &lt;= x &lt;= 1 to integer range 0..255
    276      * when dealing with colors and color alpha value
    277      * @param val float value between 0 and 1
    278      * @return null if val is null, the corresponding int if val is in the
    279      *         range 0...1. If val is outside that range, return 255
    280      */
    281     public static Integer colorFloat2int(Float val) {
    282         if (val == null)
    283             return null;
    284         if (val < 0 || val > 1)
    285             return 255;
    286         return (int) (255f * val + 0.5f);
    287     }
    288 
    289     /**
    290      * convert integer range 0..255 to float range 0 &lt;= x &lt;= 1
    291      * when dealing with colors and color alpha value
    292      * @param val integer value
    293      * @return corresponding float value in range 0 &lt;= x &lt;= 1
    294      */
    295     public static Float colorInt2float(Integer val) {
    296         if (val == null)
    297             return null;
    298         if (val < 0 || val > 255)
    299             return 1f;
    300         return ((float) val) / 255f;
    301     }
    302 
    303     /**
    304      * Multiply the alpha value of the given color with the factor. The alpha value is clamped to 0..255
    305      * @param color The color
    306      * @param alphaFactor The factor to multiply alpha with.
    307      * @return The new color.
    308      * @since 11692
    309      */
    310     public static Color alphaMultiply(Color color, float alphaFactor) {
    311         int alpha = Utils.colorFloat2int(Utils.colorInt2float(color.getAlpha()) * alphaFactor);
    312         alpha = clamp(alpha, 0, 255);
    313         return new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
    314     }
    315 
    316     /**
    317      * Returns the complementary color of {@code clr}.
    318      * @param clr the color to complement
    319      * @return the complementary color of {@code clr}
    320      */
    321     public static Color complement(Color clr) {
    322         return new Color(255 - clr.getRed(), 255 - clr.getGreen(), 255 - clr.getBlue(), clr.getAlpha());
    323     }
    324 
    325258    /**
    326259     * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe.
    327260     * @param <T> type of items
  • test/unit/org/openstreetmap/josm/tools/ColorHelperTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/ColorHelperTest.java b/test/unit/org/openstreetmap/josm/tools/ColorHelperTest.java
    index 1921bb7aa..5020add46 100644
    a b  
    1313 */
    1414public class ColorHelperTest {
    1515
     16    /**
     17     * Unit test of method {@link ColorHelper#html2color}.
     18     */
     19    @Test
     20    public void testHtml2color() {
     21        assertNull(ColorHelper.html2color(""));
     22        assertNull(ColorHelper.html2color("xyz"));
     23        assertEquals(Color.CYAN, ColorHelper.html2color("0ff"));
     24        assertEquals(Color.CYAN, ColorHelper.html2color("#0ff"));
     25        assertEquals(Color.CYAN, ColorHelper.html2color("00ffff"));
     26        assertEquals(Color.CYAN, ColorHelper.html2color("#00ffff"));
     27        assertEquals(Color.CYAN, ColorHelper.html2color("#00FFFF"));
     28        assertEquals(new Color(0x12345678, true), ColorHelper.html2color("#34567812"));
     29    }
     30
     31    /**
     32     * Unit test of method {@link ColorHelper#color2html}.
     33     */
     34    @Test
     35    public void testColor2html() {
     36        assertNull(ColorHelper.color2html(null));
     37        assertEquals("#FF0000", ColorHelper.color2html(Color.RED));
     38        assertEquals("#00FFFF", ColorHelper.color2html(Color.CYAN));
     39        assertEquals("#34567812", ColorHelper.color2html(new Color(0x12345678, true)));
     40        assertEquals("#34567812", ColorHelper.color2html(new Color(0x12345678, true), true));
     41        assertEquals("#345678", ColorHelper.color2html(new Color(0x12345678, true), false));
     42    }
     43
    1644    /**
    1745     * Unit test of method {@link ColorHelper#getForegroundColor}.
    1846     */
    public void testGetForegroundColor() {  
    2553        assertEquals(Color.BLACK, ColorHelper.getForegroundColor(Color.YELLOW));
    2654        assertEquals(Color.BLACK, ColorHelper.getForegroundColor(Color.WHITE));
    2755    }
     56
     57    /**
     58     * Test of {@link ColorHelper#float2int}
     59     */
     60    @Test
     61    public void testColorFloat2int() {
     62        assertNull(ColorHelper.float2int(null));
     63        assertEquals(255, (int) ColorHelper.float2int(-1.0f));
     64        assertEquals(0, (int) ColorHelper.float2int(-0.0f));
     65        assertEquals(0, (int) ColorHelper.float2int(0.0f));
     66        assertEquals(64, (int) ColorHelper.float2int(0.25f));
     67        assertEquals(128, (int) ColorHelper.float2int(0.5f));
     68        assertEquals(255, (int) ColorHelper.float2int(1.0f));
     69        assertEquals(255, (int) ColorHelper.float2int(2.0f));
     70    }
     71
     72    /**
     73     * Test of {@link ColorHelper#int2float}
     74     */
     75    @Test
     76    public void testColorInt2float() {
     77        assertNull(ColorHelper.int2float(null));
     78        assertEquals(1.0f, ColorHelper.int2float(-1), 1e-3);
     79        assertEquals(0.0f, ColorHelper.int2float(0), 1e-3);
     80        assertEquals(0.25f, ColorHelper.int2float(64), 1e-3);
     81        assertEquals(0.502f, ColorHelper.int2float(128), 1e-3);
     82        assertEquals(0.753f, ColorHelper.int2float(192), 1e-3);
     83        assertEquals(1.0f, ColorHelper.int2float(255), 1e-3);
     84        assertEquals(1.0f, ColorHelper.int2float(1024), 1e-3);
     85    }
     86
     87    /**
     88     * Test of {@link ColorHelper#alphaMultiply}
     89     */
     90    @Test
     91    public void testAlphaMultiply() {
     92        final Color color = new Color(0x12345678, true);
     93        assertEquals(new Color(0x12345678, true), ColorHelper.alphaMultiply(color, 1f));
     94        assertEquals(new Color(0x24345678, true), ColorHelper.alphaMultiply(color, 2f));
     95    }
     96
     97    /**
     98     * Test of {@link ColorHelper#complement}
     99     */
     100    @Test
     101    public void testComplement() {
     102        assertEquals(Color.cyan, ColorHelper.complement(Color.red));
     103        assertEquals(Color.red, ColorHelper.complement(Color.cyan));
     104        assertEquals(Color.magenta, ColorHelper.complement(Color.green));
     105        assertEquals(Color.green, ColorHelper.complement(Color.magenta));
     106        assertEquals(Color.yellow, ColorHelper.complement(Color.blue));
     107        assertEquals(Color.blue, ColorHelper.complement(Color.yellow));
     108    }
    28109}
  • test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/UtilsTest.java b/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
    index 0e5f69589..9dc4ca4fd 100644
    a b  
    77import static org.junit.Assert.assertSame;
    88import static org.junit.Assert.assertTrue;
    99
    10 import java.awt.Color;
    1110import java.io.File;
    1211import java.io.IOException;
    1312import java.util.ArrayList;
    public void testFirstNonNull() {  
    413412        assertEquals("foo", Utils.firstNonNull(null, "foo", null));
    414413    }
    415414
    416     /**
    417      * Test of {@link Utils#toString}
    418      */
    419     @Test
    420     public void testToString() {
    421         assertEquals("null", Utils.toString(null));
    422         assertEquals("#ff0000", Utils.toString(Color.red));
    423         assertEquals("#345678(alpha=18)", Utils.toString(new Color(0x12345678, true)));
    424     }
    425 
    426     /**
    427      * Test of {@link Utils#colorFloat2int}
    428      */
    429     @Test
    430     public void testColorFloat2int() {
    431         assertNull(Utils.colorFloat2int(null));
    432         assertEquals(255, (int) Utils.colorFloat2int(-1.0f));
    433         assertEquals(0, (int) Utils.colorFloat2int(-0.0f));
    434         assertEquals(0, (int) Utils.colorFloat2int(0.0f));
    435         assertEquals(64, (int) Utils.colorFloat2int(0.25f));
    436         assertEquals(128, (int) Utils.colorFloat2int(0.5f));
    437         assertEquals(255, (int) Utils.colorFloat2int(1.0f));
    438         assertEquals(255, (int) Utils.colorFloat2int(2.0f));
    439     }
    440 
    441     /**
    442      * Test of {@link Utils#colorInt2float}
    443      */
    444     @Test
    445     public void testColorInt2float() {
    446         assertNull(Utils.colorInt2float(null));
    447         assertEquals(1.0f, Utils.colorInt2float(-1), 1e-3);
    448         assertEquals(0.0f, Utils.colorInt2float(0), 1e-3);
    449         assertEquals(0.25f, Utils.colorInt2float(64), 1e-3);
    450         assertEquals(0.502f, Utils.colorInt2float(128), 1e-3);
    451         assertEquals(0.753f, Utils.colorInt2float(192), 1e-3);
    452         assertEquals(1.0f, Utils.colorInt2float(255), 1e-3);
    453         assertEquals(1.0f, Utils.colorInt2float(1024), 1e-3);
    454     }
    455 
    456     /**
    457      * Test of {@link Utils#alphaMultiply}
    458      */
    459     @Test
    460     public void testAlphaMultiply() {
    461         final Color color = new Color(0x12345678, true);
    462         assertEquals(new Color(0x12345678, true), Utils.alphaMultiply(color, 1f));
    463         assertEquals(new Color(0x24345678, true), Utils.alphaMultiply(color, 2f));
    464     }
    465 
    466     /**
    467      * Test of {@link Utils#complement}
    468      */
    469     @Test
    470     public void testComplement() {
    471         assertEquals(Color.cyan, Utils.complement(Color.red));
    472         assertEquals(Color.red, Utils.complement(Color.cyan));
    473         assertEquals(Color.magenta, Utils.complement(Color.green));
    474         assertEquals(Color.green, Utils.complement(Color.magenta));
    475         assertEquals(Color.yellow, Utils.complement(Color.blue));
    476         assertEquals(Color.blue, Utils.complement(Color.yellow));
    477     }
    478 
    479415    /**
    480416     * Test of {@link Utils#getMatches}
    481417     */