Changeset 16293 in josm for trunk


Ignore:
Timestamp:
2020-04-13T22:05:54+02:00 (5 years ago)
Author:
simon04
Message:

see #8352 - PropertiesDialog/Presets: evaluate CSS color names

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesCellRenderer.java

    r16292 r16293  
    2525import org.openstreetmap.josm.data.preferences.CachingProperty;
    2626import org.openstreetmap.josm.data.preferences.NamedColorProperty;
     27import org.openstreetmap.josm.tools.ColorHelper;
    2728import org.openstreetmap.josm.tools.I18n;
    2829import org.openstreetmap.josm.tools.Pair;
    29 import org.openstreetmap.josm.tools.Utils;
    3030
    3131/**
     
    123123                enableHTML = true;
    124124                // U+25A0 BLACK SQUARE
    125                 String escaped = Utils.escapeReservedCharactersHTML(str);
    126                 str = "<html><body><span color='" + escaped + "'>\u25A0</span> " + escaped + "</body></html>";
     125                final String color = str.matches("#[0-9A-Fa-f]{3,8}")
     126                        ? str
     127                        : ColorHelper.color2html(ColorHelper.html2color(str));
     128                str = "<html><body><span color='" + color + "'>\u25A0</span> " + color + "</body></html>";
    127129            }
    128130            ((JLabel) c).putClientProperty("html.disable", enableHTML ? null : Boolean.TRUE); // Fix #8730
  • trunk/src/org/openstreetmap/josm/tools/ColorHelper.java

    r16252 r16293  
    33
    44import java.awt.Color;
     5
     6import org.openstreetmap.josm.gui.mappaint.mapcss.CSSColors;
    57
    68/**
     
    1416
    1517    /**
    16      * Returns the {@code Color} for the given HTML code.
     18     * Returns the {@code Color} for the given HTML code, also evaluates CSS color names using {@link CSSColors}.
    1719     * @param html the color code
    1820     * @return the color
     
    2224            html = html.substring(1);
    2325        if (html.length() == 3) {
    24             return html2color(new String(
    25                     new char[]{html.charAt(0), html.charAt(0), html.charAt(1), html.charAt(1), html.charAt(2), html.charAt(2)}));
     26            html = new String(new char[]{
     27                    html.charAt(0), html.charAt(0),
     28                    html.charAt(1), html.charAt(1),
     29                    html.charAt(2), html.charAt(2)});
    2630        }
    2731        if (html.length() != 6 && html.length() != 8)
    28             return null;
     32            return CSSColors.get(html);
    2933        try {
    3034            return new Color(
     
    3438                    html.length() == 8 ? Integer.parseInt(html.substring(6, 8), 16) : 255);
    3539        } catch (NumberFormatException e) {
    36             return null;
     40            return CSSColors.get(html);
    3741        }
    3842    }
  • trunk/test/unit/org/openstreetmap/josm/tools/ColorHelperTest.java

    r16252 r16293  
    2626        assertEquals(Color.CYAN, ColorHelper.html2color("#00ffff"));
    2727        assertEquals(Color.CYAN, ColorHelper.html2color("#00FFFF"));
     28        assertEquals(Color.CYAN, ColorHelper.html2color("cyan"));
     29        assertEquals(new Color(0xa52a2a), ColorHelper.html2color("brown"));
     30        assertEquals(new Color(0x6495ed), ColorHelper.html2color("cornflowerblue"));
    2831        assertEquals(new Color(0x12345678, true), ColorHelper.html2color("#34567812"));
    2932    }
Note: See TracChangeset for help on using the changeset viewer.