Changeset 16252 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2020-04-11T08:54:12+02:00 (5 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ColorHelper.java
r9229 r16252 3 3 4 4 import java.awt.Color; 5 import java.util.Locale;6 5 7 6 /** … … 39 38 } 40 39 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 46 40 /** 47 41 * Returns the HTML color code (6 or 8 digit). … … 55 49 /** 56 50 * Returns the HTML color code (6 or 8 digit). 57 * @param col The color to convert 51 * @param color The color to convert 58 52 * @param withAlpha if {@code true} and alpha value < 255, return 8-digit color code, else always 6-digit 59 53 * @return the HTML color code (6 or 8 digit) 60 54 * @since 6655 61 55 */ 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) 64 58 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); 73 63 } 74 64 … … 86 76 Color.BLACK : Color.WHITE; 87 77 } 78 79 /** 80 * convert float range 0 <= x <= 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 <= x <= 1 96 * when dealing with colors and color alpha value 97 * @param val integer value 98 * @return corresponding float value in range 0 <= x <= 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 } 88 129 } -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r16052 r16252 6 6 import static org.openstreetmap.josm.tools.I18n.trn; 7 7 8 import java.awt.Color;9 8 import java.awt.Font; 10 9 import java.awt.font.FontRenderContext; … … 258 257 259 258 /** 260 * convert Color to String261 * (Color.toString() omits alpha value)262 * @param c the color263 * @return the String representation, including alpha264 */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 else271 return String.format("#%06x(alpha=%d)", c.getRGB() & 0x00ffffff, c.getAlpha());272 }273 274 /**275 * convert float range 0 <= x <= 1 to integer range 0..255276 * when dealing with colors and color alpha value277 * @param val float value between 0 and 1278 * @return null if val is null, the corresponding int if val is in the279 * range 0...1. If val is outside that range, return 255280 */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 <= x <= 1291 * when dealing with colors and color alpha value292 * @param val integer value293 * @return corresponding float value in range 0 <= x <= 1294 */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..255305 * @param color The color306 * @param alphaFactor The factor to multiply alpha with.307 * @return The new color.308 * @since 11692309 */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 complement319 * @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 /**326 259 * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe. 327 260 * @param <T> type of items
Note:
See TracChangeset
for help on using the changeset viewer.