Changeset 3865 in josm for trunk


Ignore:
Timestamp:
2011-02-07T11:42:34+01:00 (13 years ago)
Author:
bastiK
Message:

mapcss: basic support for node shapes and some fine tuning

Location:
trunk/src/org/openstreetmap/josm
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java

    r3862 r3865  
    22package org.openstreetmap.josm.data.osm.visitor.paint;
    33
     4import java.awt.AlphaComposite;
    45import java.awt.BasicStroke;
    56import java.awt.Color;
     
    3132import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.PolyData;
    3233import org.openstreetmap.josm.gui.NavigatableComponent;
     34import org.openstreetmap.josm.gui.mappaint.ElemStyle;
    3335import org.openstreetmap.josm.gui.mappaint.NodeElemStyle;
     36import org.openstreetmap.josm.gui.mappaint.NodeElemStyle.Symbol;
    3437import org.openstreetmap.josm.tools.ImageProvider;
    3538import org.openstreetmap.josm.tools.LanguageInfo;
     
    5457
    5558    private final Font orderFont;
    56     private final int fillAlpha;
    5759    private final int virtualNodeSize;
    5860    private final int virtualNodeSpace;
     
    8991
    9092        this.orderFont = new Font(Main.pref.get("mappaint.font", "Helvetica"), Font.PLAIN, Main.pref.getInteger("mappaint.fontsize", 8));
    91         this.fillAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
    9293        this.virtualNodeSize = virtual ? Main.pref.getInteger("mappaint.node.virtual-size", 8) / 2 : 0;
    9394        this.virtualNodeSpace = Main.pref.getInteger("mappaint.node.virtual-space", 70);
     
    187188    }
    188189
    189     public void drawNodeIcon(Node n, ImageIcon icon, boolean selected, boolean member, String name) {
     190    public void drawNodeIcon(Node n, ImageIcon icon, float iconAlpha, boolean selected, boolean member, String name) {
    190191        Point p = nc.getPoint(n);
    191192        if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
    192193
    193194        int w = icon.getIconWidth(), h=icon.getIconHeight();
     195        if (iconAlpha != 1f) {
     196            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, iconAlpha));
     197        }
    194198        icon.paintIcon ( nc, g, p.x-w/2, p.y-h/2 );
     199        g.setPaintMode();
    195200        if(name != null) {
    196201            if (inactive || n.isDisabled()) {
     
    208213            g.setColor(selected? selectedColor : relationSelectedColor);
    209214            g.drawRect(p.x-w/2-2, p.y-h/2-2, w+4, h+4);
     215        }
     216    }
     217
     218    public void drawNodeSymbol(Node n, Symbol s, boolean selected, boolean member, String name) {
     219        Point p = nc.getPoint(n);
     220        if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return;
     221        int radius = (int) (s.size / 2);
     222
     223        if (s.fillColor != null) {
     224            g.setColor(s.fillColor);
     225            switch (s.symbol) {
     226                case CIRCLE:
     227                    g.fillOval(p.x - radius, p.y - radius, (int) s.size, (int) s.size);
     228                    break;
     229                case SQUARE:
     230                    g.fillRect(p.x - radius, p.y - radius, (int) s.size, (int) s.size);
     231                    break;
     232            }
     233        }
     234        if (s.stroke != null) {
     235            g.setStroke(s.stroke);
     236            g.setColor(s.strokeColor);
     237            switch (s.symbol) {
     238                case CIRCLE:
     239                    g.drawOval(p.x - radius, p.y - radius, (int) s.size - 1, (int) s.size - 1);
     240                    break;
     241                case SQUARE:
     242                    g.drawRect(p.x - radius, p.y - radius, (int) s.size - 1, (int) s.size - 1);
     243                    break;
     244            }
     245            g.setStroke(new BasicStroke());
    210246        }
    211247    }
     
    271307            TexturePaint texture = new TexturePaint(fillImage,
    272308                    new Rectangle(polygon.xpoints[0], polygon.ypoints[0], fillImage.getWidth(), fillImage.getHeight()));
    273 
    274309            g.setPaint(texture);
     310            if (color.getAlpha() != 255) {
     311                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ElemStyle.color_int2float(color.getAlpha())));
     312            }
    275313            g.fill(polygon);
     314            g.setPaintMode();
    276315        }
    277316
  • trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java

    r3862 r3865  
    33
    44import java.awt.Color;
    5 import java.awt.Rectangle;
    6 import java.awt.TexturePaint;
    75import java.awt.image.BufferedImage;
    86
     
    1715import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    1816import org.openstreetmap.josm.tools.Utils;
    19 
    2017
    2118public class AreaElemStyle extends ElemStyle
     
    3330        BufferedImage fillImage = null;
    3431        IconReference iconRef = c.get("fill-image", null, IconReference.class);
     32        Integer fillImageAlpha = null;
     33
    3534        if (iconRef != null) {
    3635            ImageIcon icon = MapPaintStyles.getIcon(iconRef, false);
     
    4241                    throw new RuntimeException();
    4342                fillImage = (BufferedImage) icon.getImage();
     43
     44                fillImageAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255))));
     45                Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
     46                if (pAlpha != null) {
     47                    fillImageAlpha = pAlpha;
     48                }
    4449            }
    4550        }
     
    4853        if (color != null) {
    4954
    50             int alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
    51             Integer pAlpha = color_float2int(c.get("fill-opacity", null, float.class));
    52             if (pAlpha != null) {
    53                 alpha = pAlpha;
     55            int alpha;
     56            if (fillImageAlpha != null) {
     57                alpha = fillImageAlpha;
     58            } else {
     59                alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50))));
     60                Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class));
     61                if (pAlpha != null) {
     62                    alpha = pAlpha;
     63                }
    5464            }
    5565            color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha);
    5666        }
    5767       
    58         if (fillImage != null || color != null)
     68        if (fillImage != null || color != null) {
     69            if (color == null) {
     70                color = new Color(0, 0, 0, fillImageAlpha);
     71            }
    5972            return new AreaElemStyle(c, color, fillImage);
     73        }
    6074        else
    6175            return null;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/Cascade.java

    r3860 r3865  
    1515public class Cascade implements Cloneable {
    1616   
    17     public static final Cascade EMPTY_CASCADE = new Cascade();
     17    public static final Cascade EMPTY_CASCADE = new Cascade(false);
    1818
    1919    protected Map<String, Object> prop = new HashMap<String, Object>();
     20
     21    /**
     22     * constructor
     23     * @param isModifier Everything that is not on the default layer is assumed to
     24     *                   be a modifier. Can be overridden in style definition.
     25     */
     26    public Cascade(boolean isModifier) {
     27        if (isModifier) {
     28            put("modifier", true);
     29        }
     30    }
     31
     32    private Cascade() {
     33    }
    2034
    2135    public <T> T get(String key, T def, Class<T> klass) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyle.java

    r3858 r3865  
    77
    88abstract public class ElemStyle {
    9    
     9
    1010    public float z_index;
    1111    public float object_z_index;
     12    public boolean isModifier;  // false, if style can serve as main style for the
     13                                // primitive; true, if it is a highlight or modifier
    1214
    13     public ElemStyle(float z_index, float object_z_index) {
     15    public ElemStyle(float z_index, float object_z_index, boolean isModifier) {
    1416        this.z_index = z_index;
    1517        this.object_z_index = object_z_index;
     18        this.isModifier = isModifier;
    1619    }
    1720
     
    1922        z_index = c.get("z-index", 0f, Float.class);
    2023        object_z_index = c.get("object-z-index", 0f, Float.class);
     24        isModifier = c.get("modifier", false, Boolean.class);
    2125    }
    2226
     
    2832            return false;
    2933        ElemStyle s = (ElemStyle) o;
    30         return z_index == s.z_index && object_z_index == s.object_z_index;
     34        return z_index == s.z_index && object_z_index == s.object_z_index && isModifier == s.isModifier;
    3135    }
    3236
     
    3640        hash = 41 * hash + Float.floatToIntBits(this.z_index);
    3741        hash = 41 * hash + Float.floatToIntBits(this.object_z_index);
     42        hash = 41 * hash + (isModifier ? 1 : 0);
    3843        return hash;
    3944    }
     
    4247    public String toString() {
    4348        if (z_index != 0f || object_z_index != 0f)
    44             return String.format("z_idx=%s/%s ", z_index, object_z_index);
     49            return String.format("z_idx=%s/%s ", z_index, object_z_index) + (isModifier ? "modifier " : "");
    4550        return "";
    4651    }
    47 
    48     public static Integer color_float2int(Float val) {
    49         if (val == null || val < 0 || val > 1)
    50             return null;
    51         return (int) (255f * val + 0.5f);
    52     }
    53    
    54     public static Float color_int2float(Integer val) {
    55         if (val == null || val < 0 || val > 255)
    56             return null;
    57         return ((float) val) / 255f;
    58     }
    5952}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java

    r3860 r3865  
    5252        }
    5353        Pair<StyleList, Range> p = getImpl(osm, scale, nc);
    54         if (osm instanceof Node && p.a.isEmpty()) {
    55             p.a = StyleList.SIMPLE_NODE;
    56         } else if (osm instanceof Way && !Utils.exists(p.a, LineElemStyle.class)) {
    57             AreaElemStyle area = Utils.find(p.a, AreaElemStyle.class);
    58             LineElemStyle line = (area == null ? LineElemStyle.UNTAGGED_WAY : LineElemStyle.createSimpleLineStyle(area.color));
    59             p.a = new StyleList(p.a, line);
     54        if (osm instanceof Node) {
     55            boolean hasNonModifier = false;
     56            for (ElemStyle s : p.a) {
     57                if (!s.isModifier) {
     58                    hasNonModifier = true;
     59                    break;
     60                }
     61            }
     62            if (!hasNonModifier) {
     63                p.a = new StyleList(p.a, NodeElemStyle.SIMPLE_NODE_ELEMSTYLE);
     64            }
     65        } else if (osm instanceof Way) {
     66            boolean hasNonModifierLine = false;
     67            for (ElemStyle s : p.a) {
     68                if (s instanceof LineElemStyle && !s.isModifier) {
     69                    hasNonModifierLine = true;
     70                    break;
     71                }
     72            }
     73            if (!hasNonModifierLine) {
     74                AreaElemStyle area = Utils.find(p.a, AreaElemStyle.class);
     75                LineElemStyle line = (area == null ? LineElemStyle.UNTAGGED_WAY : LineElemStyle.createSimpleLineStyle(area.color));
     76                p.a = new StyleList(p.a, line);
     77            }
    6078        }
    6179        osm.mappaintStyle = osm.mappaintStyle.put(p.a, p.b);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r3860 r3865  
    2020
    2121    public static LineElemStyle createSimpleLineStyle(Color color) {
    22         Cascade c = new Cascade();
     22        Cascade c = new Cascade(false);
    2323        c.put("width", -1f);
    2424        c.put("color", color != null ? color : PaintColors.UNTAGGED.get());
     
    2929    public static final LineElemStyle UNTAGGED_WAY = createSimpleLineStyle(null);
    3030
    31     public float realWidth; // the real width of this line in meter
     31    private BasicStroke line;
    3232    public Color color;
    3333    public Color dashesBackground;
    34 
    35     private BasicStroke line;
     34    public float realWidth; // the real width of this line in meter
     35
    3636    private BasicStroke dashesLine;
    3737
     
    106106
    107107        int alpha = 255;
    108         Integer pAlpha = color_float2int(c.get("opacity", null, float.class));
     108        Integer pAlpha = Utils.color_float2int(c.get("opacity", null, float.class));
    109109        if (pAlpha != null) {
    110110            alpha = pAlpha;
     
    131131        Color dashesBackground = c.get(prefix + "dashes-background-color", null, Color.class);
    132132        if (dashesBackground != null) {
    133             pAlpha = color_float2int(c.get(prefix + "dashes-background-opacity", null, Float.class));
     133            pAlpha = Utils.color_float2int(c.get(prefix + "dashes-background-opacity", null, Float.class));
    134134            if (pAlpha != null) {
    135135                alpha = pAlpha;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MultiCascade.java

    r3836 r3865  
    1818
    1919    public Cascade getCascade(String key) {
    20         Cascade ret = get(key);
    21         if (ret == null) {
    22             ret = new Cascade();
    23             put(key, ret);
     20        if (key == null)
     21            throw new IllegalArgumentException();
     22        Cascade c = get(key);
     23        if (c == null) {
     24            c = new Cascade(!key.equals("default"));
     25            put(key, c);
    2426        }
    25         return ret;
     27        return c;
    2628    }
    2729
  • trunk/src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java

    r3862 r3865  
    22package org.openstreetmap.josm.gui.mappaint;
    33
     4import static org.openstreetmap.josm.tools.Utils.equal;
     5
     6import java.awt.BasicStroke;
    47import java.awt.Color;
     8import java.awt.Stroke;
    59
    610import javax.swing.GrayFilter;
    711import javax.swing.ImageIcon;
    812
     13import org.openstreetmap.josm.Main;
    914import org.openstreetmap.josm.data.osm.Node;
    1015import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    2227    public String annotation_key;
    2328    public ImageIcon icon;
     29    public int iconAlpha;
     30    public Symbol symbol;
     31
    2432    private ImageIcon disabledIcon;
    2533
    26     public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE = new NodeElemStyle(Cascade.EMPTY_CASCADE, true, null, null);
    27 
    28     protected NodeElemStyle(Cascade c, boolean annotate, String annotation_key, ImageIcon icon) {
     34    public static class Symbol {
     35        public SymbolShape symbol;
     36        public float size;
     37        public Stroke stroke;
     38        public Color strokeColor;
     39        public Color fillColor;
     40
     41        public Symbol(SymbolShape symbol, float size, Stroke stroke, Color strokeColor, Color fillColor) {
     42            if (stroke != null && strokeColor == null)
     43                throw new IllegalArgumentException();
     44            if (stroke == null && fillColor == null)
     45                throw new IllegalArgumentException();
     46            this.symbol = symbol;
     47            this.size = size;
     48            this.stroke = stroke;
     49            this.strokeColor = strokeColor;
     50            this.fillColor = fillColor;
     51        }
     52
     53        @Override
     54        public boolean equals(Object obj) {
     55            if (obj == null || getClass() != obj.getClass())
     56                return false;
     57            final Symbol other = (Symbol) obj;
     58            return  symbol == other.symbol &&
     59                    size == other.size &&
     60                    equal(stroke, other.stroke) &&
     61                    equal(strokeColor, other.strokeColor) &&
     62                    equal(fillColor, other.fillColor);
     63        }
     64
     65        @Override
     66        public int hashCode() {
     67            int hash = 7;
     68            hash = 67 * hash + symbol.hashCode();
     69            hash = 67 * hash + Float.floatToIntBits(size);
     70            hash = 67 * hash + (stroke != null ? stroke.hashCode() : 0);
     71            hash = 67 * hash + (strokeColor != null ? strokeColor.hashCode() : 0);
     72            hash = 67 * hash + (fillColor != null ? fillColor.hashCode() : 0);
     73            return hash;
     74        }
     75
     76        @Override
     77        public String toString() {
     78            return "symbol=" + symbol + " size=" + size +
     79                    (stroke != null ? (" stroke=" + stroke + " strokeColor=" + strokeColor) : "") +
     80                    (fillColor != null ? (" fillColor=" + fillColor) : "");
     81        }
     82    }
     83   
     84    public enum SymbolShape { SQUARE, CIRCLE }
     85
     86    public static final NodeElemStyle SIMPLE_NODE_ELEMSTYLE = new NodeElemStyle(Cascade.EMPTY_CASCADE, true, null, null, 0, null);
     87
     88    protected NodeElemStyle(Cascade c, boolean annotate, String annotation_key, ImageIcon icon, int iconAlpha, Symbol symbol) {
    2989        super(c);
    3090        this.annotate = annotate;
    3191        this.annotation_key = annotation_key;
    3292        this.icon = icon;
     93        this.iconAlpha = iconAlpha;
     94        this.symbol = symbol;
    3395    }
    3496
    3597    public static NodeElemStyle create(Cascade c) {
    3698        IconReference iconRef = c.get("icon-image", null, IconReference.class);
    37         if (iconRef == null)
    38             return null;
    39         ImageIcon icon = MapPaintStyles.getIcon(iconRef, false);
    40        
     99        ImageIcon icon = null;
     100        int iconAlpha = 0;
     101        Symbol symbol = null;
     102
     103        if (iconRef != null) {
     104            icon = MapPaintStyles.getIcon(iconRef, false);
     105            iconAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255))));
     106            Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class));
     107            if (pAlpha != null) {
     108                iconAlpha = pAlpha;
     109            }
     110        } else {
     111            SymbolShape shape;
     112            String shapeStr = c.get("symbol-shape", null, String.class);
     113            if (equal(shapeStr, "square")) {
     114                shape = SymbolShape.SQUARE;
     115            } else if (equal(shapeStr, "circle")) {
     116                shape = SymbolShape.CIRCLE;
     117            } else
     118                return null;
     119
     120            Float size = c.get("symbol-size", null, Float.class);
     121            if (size == null || size <= 0)
     122                return null;
     123
     124            Float strokeWidth = c.get("symbol-stroke-width", null, Float.class);
     125            Color strokeColor = c.get("symbol-stroke-color", null, Color.class);
     126            if (strokeColor != null) {
     127                float strokeAlpha = c.get("symbol-stroke-opacity", 1f, Float.class);
     128                strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(),
     129                        strokeColor.getBlue(), Utils.color_float2int(strokeAlpha));
     130            }
     131            Stroke stroke = null;
     132            if (strokeWidth != null && strokeWidth > 0 && strokeColor != null) {
     133                stroke = new BasicStroke(strokeWidth);
     134            }
     135
     136            Color fillColor = c.get("symbol-fill-color", null, Color.class);
     137            if (fillColor != null) {
     138                float fillAlpha = c.get("symbol-fill-opacity", 1f, Float.class);
     139                fillColor = new Color(fillColor.getRed(), fillColor.getGreen(),
     140                        fillColor.getBlue(), Utils.color_float2int(fillAlpha));
     141            }
     142
     143            if ((stroke == null || strokeColor == null) && fillColor == null)
     144                return null;
     145
     146            symbol = new Symbol(shape, size, stroke, strokeColor, fillColor);
     147        }
     148
    41149        String text = c.get("text", null, String.class);
    42150
     
    44152        String annotation_key = null;
    45153
    46         if (annotate && !"yes".equalsIgnoreCase(text)) {
     154        if (annotate && !"auto".equalsIgnoreCase(text)) {
    47155            annotation_key = text;
    48156        }
    49         return new NodeElemStyle(c, annotate, annotation_key, icon);
     157        return new NodeElemStyle(c, annotate, annotation_key, icon, iconAlpha, symbol);
    50158    }
    51159
     
    56164            if (icon != null && painter.isShowIcons()) {
    57165                painter.drawNodeIcon(n, (painter.isInactive() || n.isDisabled()) ? getDisabledIcon() : icon,
    58                         selected, member, getName(n, painter));
     166                        Utils.color_int2float(iconAlpha), selected, member, getName(n, painter));
     167            } else if (symbol != null) {
     168                painter.drawNodeSymbol(n, symbol, selected, member, getName(n, painter));
    59169            } else {
    60170                if (n.isHighlighted()) {
     
    127237        hash = 17 * hash + (annotation_key != null ? annotation_key.hashCode() : 0);
    128238        hash = 17 * hash + (icon != null ? icon.getImage().hashCode() : 0);
     239        hash = 17 * hash + this.iconAlpha;
     240        hash = 17 * hash + (this.symbol != null ? this.symbol.hashCode() : 0);
    129241        return hash;
    130242    }
     
    143255        if (annotate != other.annotate)
    144256            return false;
    145         if (!Utils.equal(annotation_key, annotation_key))
     257        if (!equal(annotation_key, annotation_key))
     258            return false;
     259        if (this.iconAlpha != other.iconAlpha)
     260            return false;
     261        if (!equal(symbol, other.symbol))
    146262            return false;
    147263        return true;
    148264    }
    149265
     266
    150267    @Override
    151268    public String toString() {
    152         return "NodeElemStyle{" + super.toString() + "annotate=" + annotate + " annotation_key=" + annotation_key + " icon=" + icon + '}';
     269        return "NodeElemStyle{" + super.toString() + "annotate=" + annotate + " annotation_key=" + annotation_key +
     270                (icon != null ? (" icon=" + icon + " iconAlpha=" + iconAlpha) : "") +
     271                (symbol != null ? (" symbol=[" + symbol + "]") : "") + '}';
    153272    }
    154273
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleCache.java

    r3836 r3865  
    4848    {
    4949        private List<ElemStyle> lst;
    50 
    51         public static final StyleList SIMPLE_NODE = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE);
    5250
    5351        public StyleList() {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r3863 r3865  
    129129                            c = mc.get("*").clone();
    130130                        } else {
    131                             c = new Cascade();
     131                            c = new Cascade(!sub.equals("default"));
    132132                        }
    133133                        mc.put(sub, c);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java

    r3863 r3865  
    2020import org.openstreetmap.josm.data.osm.Way;
    2121import org.openstreetmap.josm.gui.mappaint.Cascade;
    22 import org.openstreetmap.josm.gui.mappaint.ElemStyle;
    2322import org.openstreetmap.josm.gui.mappaint.MultiCascade;
    2423import org.openstreetmap.josm.gui.mappaint.Range;
     
    289288                    if (icon.annotate != null) {
    290289                        if (icon.annotate) {
    291                             def.put("text", "yes");
     290                            def.put("text", "auto");
    292291                        } else {
    293292                            def.remove("text");
     
    306305                    int alpha = p.line.color.getAlpha();
    307306                    if (alpha != 255) {
    308                         def.put("opacity", ElemStyle.color_int2float(alpha));
     307                        def.put("opacity", Utils.color_int2float(alpha));
    309308                    }
    310309                }
     
    335334                        int alpha = mod.color.getAlpha();
    336335                        if (alpha != 255) {
    337                             c.put("opacity", ElemStyle.color_int2float(alpha));
     336                            c.put("opacity", Utils.color_int2float(alpha));
    338337                        }
    339338                    }
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r3859 r3865  
    124124            return String.format("#%06x(alpha=%d)", c.getRGB() & 0x00ffffff, c.getAlpha());
    125125    }
     126
     127    /**
     128     * convert float range 0 <= x <= 1 to integer range 0..255
     129     * when dealing with colors and color alpha value
     130     */
     131    public static Integer color_float2int(Float val) {
     132        if (val == null)
     133            return null;
     134        if (val < 0 || val > 1)
     135            return 255;
     136        return (int) (255f * val + 0.5f);
     137    }
     138
     139    /**
     140     * convert back
     141     */
     142    public static Float color_int2float(Integer val) {
     143        if (val == null)
     144            return null;
     145        if (val < 0 || val > 255)
     146            return 1f;
     147        return ((float) val) / 255f;
     148    }
     149
    126150}
Note: See TracChangeset for help on using the changeset viewer.