Ignore:
Timestamp:
2020-04-11T08:54:12+02:00 (4 years ago)
Author:
simon04
Message:

fix #18961 - ColorHelper: harmonize color functions

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ColorHelper.java

    r9229 r16252  
    33
    44import java.awt.Color;
    5 import java.util.Locale;
    65
    76/**
     
    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).
     
    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 < 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
     
    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}
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r16052 r16252  
    66import static org.openstreetmap.josm.tools.I18n.trn;
    77
    8 import java.awt.Color;
    98import java.awt.Font;
    109import java.awt.font.FontRenderContext;
     
    258257
    259258    /**
    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 
    325     /**
    326259     * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe.
    327260     * @param <T> type of items
Note: See TracChangeset for help on using the changeset viewer.