| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import java.awt.*; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class ColorUtil { |
| 25 |
private static int shift(int colorComponent, double d) { |
| 26 |
final int n = (int) (colorComponent * d); |
| 27 |
return n > 255 ? 255 : n < 0 ? 0 : n; |
| 28 |
} |
| 29 |
|
| 30 |
public static Color shift(Color c, double d) { |
| 31 |
return new Color(shift(c.getRed(), d), shift(c.getGreen(), d), shift(c.getBlue(), d), c.getAlpha()); |
| 32 |
} |
| 33 |
|
| 34 |
public static Color toAlpha(Color color, int a) { |
| 35 |
Color c = color != null ? color : Color.black; |
| 36 |
return new Color(c.getRed(), c.getGreen(), c.getBlue(), a); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public static Color fromHex(String str) { |
| 50 |
if (str.startsWith("#")) { |
| 51 |
str = str.substring(1); |
| 52 |
} |
| 53 |
if (str.length() == 3) { |
| 54 |
return new Color( |
| 55 |
17 * Integer.valueOf(String.valueOf(str.charAt(0)), 16), |
| 56 |
17 * Integer.valueOf(String.valueOf(str.charAt(1)), 16), |
| 57 |
17 * Integer.valueOf(String.valueOf(str.charAt(2)), 16)); |
| 58 |
} else if (str.length() == 6) { |
| 59 |
return Color.decode("0x" + str); |
| 60 |
} else { |
| 61 |
throw new IllegalArgumentException("Should be String of 3 or 6 chars length."); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public static Color fromHex(String str, Color defaultValue) { |
| 66 |
try { |
| 67 |
return fromHex(str); |
| 68 |
} catch (Exception e) { |
| 69 |
return defaultValue; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static boolean isDark(final Color c) { |
| 81 |
|
| 82 |
return (1 - (0.299 * c.getRed() + 0.587 * c.getGreen() + 0.114 * c.getBlue()) / 255) >= 0.5; |
| 83 |
} |
| 84 |
} |