Ticket #18961: 18961.patch
File 18961.patch, 29.5 KB (added by , 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 25 25 import org.openstreetmap.josm.data.osm.Way; 26 26 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors; 27 27 import org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable; 28 import org.openstreetmap.josm.tools. Utils;28 import org.openstreetmap.josm.tools.ColorHelper; 29 29 30 30 /** 31 31 * Manages the selection of a rectangle or a lasso loop. Listening to left and right mouse button … … 82 82 public void paint(Graphics2D g, MapView mv, Bounds bbox) { 83 83 if (mousePos == null || mousePosStart == null || mousePos == mousePosStart) 84 84 return; 85 Color color = Utils.complement(PaintColors.getBackgroundColor());85 Color color = ColorHelper.complement(PaintColors.getBackgroundColor()); 86 86 g.setColor(color); 87 87 if (lassoMode) { 88 88 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 74 74 import org.openstreetmap.josm.gui.widgets.JosmTextArea; 75 75 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; 76 76 import org.openstreetmap.josm.gui.widgets.ScrollableTable; 77 import org.openstreetmap.josm.tools.ColorHelper; 77 78 import org.openstreetmap.josm.tools.GBC; 78 79 import org.openstreetmap.josm.tools.ImageOverlay; 79 80 import org.openstreetmap.josm.tools.ImageProvider; … … private JPanel buildInfoPanel(StyleSource s) { 611 612 text.append(tableRow(tr("Icon:"), s.icon)); 612 613 } 613 614 if (s.getBackgroundColorOverride() != null) { 614 text.append(tableRow(tr("Background:"), Utils.toString(s.getBackgroundColorOverride())));615 text.append(tableRow(tr("Background:"), ColorHelper.color2html(s.getBackgroundColorOverride()))); 615 616 } 616 617 text.append(tableRow(tr("Style is currently active?"), s.active ? tr("Yes") : tr("No"))) 617 618 .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 14 14 import org.openstreetmap.josm.tools.ColorHelper; 15 15 import org.openstreetmap.josm.tools.GenericParser; 16 16 import org.openstreetmap.josm.tools.Logging; 17 import org.openstreetmap.josm.tools.Utils;18 17 19 18 /** 20 19 * Simple map of properties with dynamic typing. … … public String toString() { 250 249 if (val instanceof float[]) { 251 250 sb.append(Arrays.toString((float[]) val)); 252 251 } else if (val instanceof Color) { 253 sb.append( Utils.toString((Color) val));252 sb.append(ColorHelper.color2html((Color) val)); 254 253 } else if (val != null) { 255 254 sb.append(val); 256 255 } -
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) 270 270 * @see java.awt.Color#getRed() 271 271 */ 272 272 public static float red(Color c) { // NO_UCD (unused code) 273 return Utils.colorInt2float(c.getRed());273 return ColorHelper.int2float(c.getRed()); 274 274 } 275 275 276 276 /** … … public static float red(Color c) { // NO_UCD (unused code) 280 280 * @see java.awt.Color#getGreen() 281 281 */ 282 282 public static float green(Color c) { // NO_UCD (unused code) 283 return Utils.colorInt2float(c.getGreen());283 return ColorHelper.int2float(c.getGreen()); 284 284 } 285 285 286 286 /** … … public static float green(Color c) { // NO_UCD (unused code) 290 290 * @see java.awt.Color#getBlue() 291 291 */ 292 292 public static float blue(Color c) { // NO_UCD (unused code) 293 return Utils.colorInt2float(c.getBlue());293 return ColorHelper.int2float(c.getBlue()); 294 294 } 295 295 296 296 /** … … public static float blue(Color c) { // NO_UCD (unused code) 300 300 * @see java.awt.Color#getAlpha() 301 301 */ 302 302 public static float alpha(Color c) { // NO_UCD (unused code) 303 return Utils.colorInt2float(c.getAlpha());303 return ColorHelper.int2float(c.getAlpha()); 304 304 } 305 305 306 306 /** -
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 17 17 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 18 18 import org.openstreetmap.josm.spi.preferences.Config; 19 19 import org.openstreetmap.josm.tools.CheckParameterUtil; 20 import org.openstreetmap.josm.tools.ColorHelper; 20 21 import org.openstreetmap.josm.tools.HiDPISupport; 21 22 import org.openstreetmap.josm.tools.Utils; 22 23 import org.openstreetmap.josm.tools.bugreport.BugReport; … … public static AreaElement create(final Environment env) { 95 96 } 96 97 97 98 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)); 99 100 if (pAlpha != null) { 100 101 fillImage.alpha = pAlpha; 101 102 } 102 103 } else { 103 104 color = c.get(FILL_COLOR, null, Color.class); 104 105 if (color != null) { 105 float defaultOpacity = Utils.colorInt2float(DEFAULT_FILL_ALPHA.get());106 float defaultOpacity = ColorHelper.int2float(DEFAULT_FILL_ALPHA.get()); 106 107 float opacity = c.get(FILL_OPACITY, defaultOpacity, Float.class); 107 color = Utils.alphaMultiply(color, opacity);108 color = ColorHelper.alphaMultiply(color, opacity); 108 109 } 109 110 } 110 111 … … public int hashCode() { 158 159 159 160 @Override 160 161 public String toString() { 161 return "AreaElemStyle{" + super.toString() + "color=" + Utils.toString(color) +162 return "AreaElemStyle{" + super.toString() + "color=" + ColorHelper.color2html(color) + 162 163 " fillImage=[" + fillImage + "] extent=[" + extent + "] extentThreshold=[" + extentThreshold + "]}"; 163 164 } 164 165 } -
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 19 19 import org.openstreetmap.josm.gui.mappaint.Keyword; 20 20 import org.openstreetmap.josm.gui.mappaint.MultiCascade; 21 21 import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction.RelativeFloat; 22 import org.openstreetmap.josm.tools.ColorHelper; 22 23 import org.openstreetmap.josm.tools.Logging; 23 import org.openstreetmap.josm.tools.Utils;24 24 25 25 /** 26 26 * This is the style definition for a simple line. … … public int hashCode() { 234 234 @Override 235 235 public String toString() { 236 236 return "LineElemStyle{" + super.toString() + "width=" + line.getLineWidth() + 237 " realWidth=" + realWidth + " color=" + Utils.toString(color) +237 " realWidth=" + realWidth + " color=" + ColorHelper.color2html(color) + 238 238 " dashed=" + Arrays.toString(line.getDashArray()) + 239 239 (line.getDashPhase() == 0 ? "" : " dashesOffses=" + line.getDashPhase()) + 240 " dashedColor=" + Utils.toString(dashesBackground) +240 " dashedColor=" + ColorHelper.color2html(dashesBackground) + 241 241 " linejoin=" + linejoinToString(line.getLineJoin()) + 242 242 " linecap=" + linecapToString(line.getEndCap()) + 243 243 (offset == 0 ? "" : " offset=" + offset) + … … private static LineElement createImpl(Environment env, LineType type) { 333 333 color = PaintColors.UNTAGGED.get(); 334 334 } 335 335 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)); 337 337 if (pAlpha != null) { 338 338 alpha = pAlpha; 339 339 } … … private static LineElement createImpl(Environment env, LineType type) { 362 362 } 363 363 Color dashesBackground = c.get(type.prefix + DASHES_BACKGROUND_COLOR, null, Color.class); 364 364 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)); 366 366 if (pAlpha != null) { 367 367 alpha = pAlpha; 368 368 } -
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 21 21 import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProvider; 22 22 import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProviderResult; 23 23 import org.openstreetmap.josm.gui.util.GuiHelper; 24 import org.openstreetmap.josm.tools.ColorHelper; 24 25 import org.openstreetmap.josm.tools.ImageProvider; 25 26 import org.openstreetmap.josm.tools.ImageResource; 26 27 import org.openstreetmap.josm.tools.Logging; 27 import org.openstreetmap.josm.tools.Utils;28 28 29 29 /** 30 30 * An image that will be displayed on the map. … … public int getHeight() { 214 214 * @return The value in range 0..1 215 215 */ 216 216 public float getAlphaFloat() { 217 return Utils.colorInt2float(alpha);217 return ColorHelper.int2float(alpha); 218 218 } 219 219 220 220 /** -
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 23 23 import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.SimpleBoxProvider; 24 24 import org.openstreetmap.josm.spi.preferences.Config; 25 25 import org.openstreetmap.josm.tools.CheckParameterUtil; 26 import org.openstreetmap.josm.tools.ColorHelper; 26 27 import org.openstreetmap.josm.tools.Logging; 27 28 import org.openstreetmap.josm.tools.RotationAngle; 28 29 import org.openstreetmap.josm.tools.Utils; … … public static MapImage createIcon(final Environment env, final String... keys) { 167 168 mapImage.offsetY = Math.round(offsetYF); 168 169 169 170 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)); 171 172 if (pAlpha != null) { 172 173 mapImage.alpha = pAlpha; 173 174 } … … private static Symbol createSymbol(Environment env) { 212 213 213 214 Stroke stroke = null; 214 215 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)); 216 217 if (strokeAlpha != null) { 217 218 strokeColor = new Color(strokeColor.getRed(), strokeColor.getGreen(), 218 219 strokeColor.getBlue(), strokeAlpha); … … private static Symbol createSymbol(Environment env) { 226 227 } 227 228 228 229 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)); 230 231 if (fillAlpha != null) { 231 232 fillColor = new Color(fillColor.getRed(), fillColor.getGreen(), 232 233 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 17 17 import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.StaticLabelCompositionStrategy; 18 18 import org.openstreetmap.josm.gui.mappaint.styleelement.LabelCompositionStrategy.TagLookupCompositionStrategy; 19 19 import org.openstreetmap.josm.tools.CheckParameterUtil; 20 import org.openstreetmap.josm.tools. Utils;20 import org.openstreetmap.josm.tools.ColorHelper; 21 21 22 22 /** 23 23 * Represents the rendering style for a textual label placed somewhere on the map. … … public static TextLabel create(Environment env, Color defaultTextColor, boolean 139 139 140 140 Color color = c.get(TEXT_COLOR, defaultTextColor, Color.class); 141 141 float alpha = c.get(TEXT_OPACITY, 1f, Float.class); 142 color = Utils.alphaMultiply(color, alpha);142 color = ColorHelper.alphaMultiply(color, alpha); 143 143 144 144 Float haloRadius = c.get(TEXT_HALO_RADIUS, null, Float.class); 145 145 if (haloRadius != null && haloRadius <= 0) { … … public static TextLabel create(Environment env, Color defaultTextColor, boolean 147 147 } 148 148 Color haloColor = null; 149 149 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); 151 151 float haloAlphaFactor = c.get(TEXT_HALO_OPACITY, 1f, Float.class); 152 haloColor = Utils.alphaMultiply(haloColor, haloAlphaFactor);152 haloColor = ColorHelper.alphaMultiply(haloColor, haloAlphaFactor); 153 153 } 154 154 155 155 return new TextLabel(strategy, font, color, haloRadius, haloColor); … … protected String toStringImpl() { 198 198 StringBuilder sb = new StringBuilder(96); 199 199 sb.append("labelCompositionStrategy=").append(labelCompositionStrategy) 200 200 .append(" font=").append(font) 201 .append(" color=").append( Utils.toString(color));201 .append(" color=").append(ColorHelper.color2html(color)); 202 202 if (haloRadius != null) { 203 203 sb.append(" haloRadius=").append(haloRadius) 204 204 .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 2 2 package org.openstreetmap.josm.tools; 3 3 4 4 import java.awt.Color; 5 import java.util.Locale;6 5 7 6 /** 8 7 * Helper to convert from color to HTML string and back. … … public static Color html2color(String html) { 38 37 } 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). 48 42 * @param col The color to convert … … public static String color2html(Color col) { 54 48 55 49 /** 56 50 * Returns the HTML color code (6 or 8 digit). 57 * @param col The color to convert51 * @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 75 65 /** … … public static Color getForegroundColor(Color bg) { 85 75 (bg.getRed()*0.299 + bg.getGreen()*0.587 + bg.getBlue()*0.114) > 186 ? 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 } -
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 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 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; 11 10 import java.awt.font.GlyphVector; … … public static String joinAsHtmlUnorderedList(Iterable<?> values) { 256 255 return StreamUtils.toStream(values).map(Object::toString).collect(StreamUtils.toHtmlList()); 257 256 } 258 257 259 /**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 258 /** 326 259 * Copies the given array. Unlike {@link Arrays#copyOf}, this method is null-safe. 327 260 * @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 13 13 */ 14 14 public class ColorHelperTest { 15 15 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 16 44 /** 17 45 * Unit test of method {@link ColorHelper#getForegroundColor}. 18 46 */ … … public void testGetForegroundColor() { 25 53 assertEquals(Color.BLACK, ColorHelper.getForegroundColor(Color.YELLOW)); 26 54 assertEquals(Color.BLACK, ColorHelper.getForegroundColor(Color.WHITE)); 27 55 } 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 } 28 109 } -
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 7 7 import static org.junit.Assert.assertSame; 8 8 import static org.junit.Assert.assertTrue; 9 9 10 import java.awt.Color;11 10 import java.io.File; 12 11 import java.io.IOException; 13 12 import java.util.ArrayList; … … public void testFirstNonNull() { 413 412 assertEquals("foo", Utils.firstNonNull(null, "foo", null)); 414 413 } 415 414 416 /**417 * Test of {@link Utils#toString}418 */419 @Test420 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 @Test430 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 @Test445 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 @Test460 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 @Test470 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 479 415 /** 480 416 * Test of {@link Utils#getMatches} 481 417 */