| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
package jalview.util; |
| 26 |
|
|
| 27 |
|
import java.awt.Color; |
| 28 |
|
import java.util.ArrayList; |
| 29 |
|
import java.util.Collections; |
| 30 |
|
import java.util.HashMap; |
| 31 |
|
import java.util.List; |
| 32 |
|
import java.util.Locale; |
| 33 |
|
import java.util.Map; |
| 34 |
|
import java.util.Map.Entry; |
| 35 |
|
import java.util.Random; |
| 36 |
|
|
| 37 |
|
import org.jcolorbrewer.ColorBrewer; |
| 38 |
|
|
| 39 |
|
import jalview.bin.Console; |
| 40 |
|
|
| |
|
| 0% |
Uncovered Elements: 362 (362) |
Complexity: 89 |
Complexity Density: 0.34 |
|
| 41 |
|
public class ColorUtils |
| 42 |
|
{ |
| 43 |
|
private static final int MAX_CACHE_SIZE = 1729; |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
private static Map<String, Color> myColours = new HashMap<>(); |
| 49 |
|
|
| 50 |
|
private static Map<String, Color> myHSBSpacedColours = new HashMap<>(); |
| 51 |
|
|
| 52 |
|
private static final double GOLDEN_RATIO_CONJUGATE = 0.6180339887; |
| 53 |
|
|
| 54 |
|
private static final float DEFAULT_HSB_DISTANCE_THRESHOLD = 0.5f; |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
private static float HSB_DISTANCE_THRESHOLD = DEFAULT_HSB_DISTANCE_THRESHOLD; |
| 59 |
|
|
| 60 |
|
private static final float DECREMENT_VAL_FOR_HSB_DISTANCE_THRESHOLD = 0.01f; |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
private static final int MAX_ITERATION_FOR_ADJUSTING_COLOR = 100; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
private static final int POS_HUE = 0; |
| 83 |
|
|
| 84 |
|
private static final int POS_SATURATION = 1; |
| 85 |
|
|
| 86 |
|
private static final int POS_BRIGHTNESS = 2; |
| 87 |
|
|
| 88 |
|
private static final float HSB_HIGH_SATURATION = 0.75f; |
| 89 |
|
private static final float HSB_HIGH_BRIGHTNESS = 0.9f; |
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
@param |
| 99 |
|
@return |
| 100 |
|
|
| |
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 101 |
0 |
public static final Color generateRandomColor(Color mix)... |
| 102 |
|
{ |
| 103 |
0 |
Random random = new Random(); |
| 104 |
0 |
int red = random.nextInt(256); |
| 105 |
0 |
int green = random.nextInt(256); |
| 106 |
0 |
int blue = random.nextInt(256); |
| 107 |
|
|
| 108 |
|
|
| 109 |
0 |
if (mix != null) |
| 110 |
|
{ |
| 111 |
0 |
red = (red + mix.getRed()) / 2; |
| 112 |
0 |
green = (green + mix.getGreen()) / 2; |
| 113 |
0 |
blue = (blue + mix.getBlue()) / 2; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
0 |
Color color = new Color(red, green, blue); |
| 117 |
0 |
return color; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
@return |
| 123 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 124 |
0 |
public static final Color getARandomColor()... |
| 125 |
|
{ |
| 126 |
0 |
Color col = new Color((int) (Math.random() * 255), |
| 127 |
|
(int) (Math.random() * 255), (int) (Math.random() * 255)); |
| 128 |
0 |
return col; |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
@param |
| 136 |
|
@return |
| 137 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 138 |
0 |
public static Color getColorForIndex(int index)... |
| 139 |
|
{ |
| 140 |
|
|
| 141 |
0 |
float hue = (index * (float) GOLDEN_RATIO_CONJUGATE) % 1.0f; |
| 142 |
0 |
return Color.getHSBColor(hue, HSB_HIGH_SATURATION, HSB_HIGH_BRIGHTNESS); |
| 143 |
|
|
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
@param |
| 150 |
|
@return |
| 151 |
|
@see |
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| |
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 4 |
Complexity Density: 2 |
|
| 155 |
0 |
public static final String toTkCode(Color colour)... |
| 156 |
|
{ |
| 157 |
0 |
String colstring = "#" + ((colour.getRed() < 16) ? "0" : "") |
| 158 |
|
+ Integer.toHexString(colour.getRed()) |
| 159 |
0 |
+ ((colour.getGreen() < 16) ? "0" : "") |
| 160 |
|
+ Integer.toHexString(colour.getGreen()) |
| 161 |
0 |
+ ((colour.getBlue() < 16) ? "0" : "") |
| 162 |
|
+ Integer.toHexString(colour.getBlue()); |
| 163 |
0 |
return colstring; |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
@param |
| 171 |
|
@return |
| 172 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
| 173 |
0 |
public static Color darkerThan(Color col)... |
| 174 |
|
{ |
| 175 |
0 |
return col == null ? null : col.darker().darker().darker(); |
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
@param |
| 183 |
|
@return |
| 184 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
| 185 |
0 |
public static Color brighterThan(Color col)... |
| 186 |
|
{ |
| 187 |
0 |
return col == null ? null : col.brighter().brighter().brighter(); |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
|
| 191 |
|
|
| 192 |
|
|
| 193 |
|
|
| 194 |
|
@param |
| 195 |
|
@param |
| 196 |
|
@param |
| 197 |
|
@param |
| 198 |
|
@param |
| 199 |
|
@return |
| 200 |
|
|
| |
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 201 |
0 |
public static Color getGraduatedColour(float value, float minValue,... |
| 202 |
|
Color minColour, float maxValue, Color maxColour) |
| 203 |
|
{ |
| 204 |
0 |
if (minValue == maxValue) |
| 205 |
|
{ |
| 206 |
0 |
return minColour; |
| 207 |
|
} |
| 208 |
0 |
if (value < minValue) |
| 209 |
|
{ |
| 210 |
0 |
value = minValue; |
| 211 |
|
} |
| 212 |
0 |
if (value > maxValue) |
| 213 |
|
{ |
| 214 |
0 |
value = maxValue; |
| 215 |
|
} |
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
0 |
float prop = (value - minValue) / (maxValue - minValue); |
| 221 |
0 |
float r = minColour.getRed() |
| 222 |
|
+ prop * (maxColour.getRed() - minColour.getRed()); |
| 223 |
0 |
float g = minColour.getGreen() |
| 224 |
|
+ prop * (maxColour.getGreen() - minColour.getGreen()); |
| 225 |
0 |
float b = minColour.getBlue() |
| 226 |
|
+ prop * (maxColour.getBlue() - minColour.getBlue()); |
| 227 |
0 |
return new Color(r / 255, g / 255, b / 255); |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
@param |
| 240 |
|
@param |
| 241 |
|
@return |
| 242 |
|
|
| |
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 5 |
Complexity Density: 0.26 |
|
| 243 |
0 |
public static Color bleachColour(Color colour, float bleachFactor)... |
| 244 |
|
{ |
| 245 |
0 |
if (bleachFactor >= 1f) |
| 246 |
|
{ |
| 247 |
0 |
return Color.WHITE; |
| 248 |
|
} |
| 249 |
0 |
if (bleachFactor <= -1f) |
| 250 |
|
{ |
| 251 |
0 |
return Color.BLACK; |
| 252 |
|
} |
| 253 |
0 |
if (bleachFactor == 0f) |
| 254 |
|
{ |
| 255 |
0 |
return colour; |
| 256 |
|
} |
| 257 |
|
|
| 258 |
0 |
int red = colour.getRed(); |
| 259 |
0 |
int green = colour.getGreen(); |
| 260 |
0 |
int blue = colour.getBlue(); |
| 261 |
|
|
| 262 |
0 |
if (bleachFactor > 0) |
| 263 |
|
{ |
| 264 |
0 |
red += (255 - red) * bleachFactor; |
| 265 |
0 |
green += (255 - green) * bleachFactor; |
| 266 |
0 |
blue += (255 - blue) * bleachFactor; |
| 267 |
0 |
return new Color(red, green, blue); |
| 268 |
|
} |
| 269 |
|
else |
| 270 |
|
{ |
| 271 |
0 |
float factor = 1 + bleachFactor; |
| 272 |
0 |
red *= factor; |
| 273 |
0 |
green *= factor; |
| 274 |
0 |
blue *= factor; |
| 275 |
0 |
return new Color(red, green, blue); |
| 276 |
|
} |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
|
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
@param |
| 288 |
|
@return |
| 289 |
|
|
| |
|
| 0% |
Uncovered Elements: 33 (33) |
Complexity: 9 |
Complexity Density: 0.43 |
|
| 290 |
0 |
public static Color parseColourString(String colour)... |
| 291 |
|
{ |
| 292 |
0 |
if (colour == null) |
| 293 |
|
{ |
| 294 |
0 |
return null; |
| 295 |
|
} |
| 296 |
0 |
colour = colour.trim(); |
| 297 |
|
|
| 298 |
0 |
Color col = null; |
| 299 |
|
|
| 300 |
0 |
if ("random".equals(colour)) |
| 301 |
|
{ |
| 302 |
0 |
return generateRandomColor(null); |
| 303 |
|
} |
| 304 |
|
|
| 305 |
0 |
if (StringUtils.isHexString(colour)) |
| 306 |
|
{ |
| 307 |
0 |
try |
| 308 |
|
{ |
| 309 |
0 |
int value = Integer.parseInt(colour, 16); |
| 310 |
0 |
col = new Color(value); |
| 311 |
|
} catch (NumberFormatException ex) |
| 312 |
|
{ |
| 313 |
|
} |
| 314 |
|
} |
| 315 |
|
|
| 316 |
0 |
if (col == null) |
| 317 |
|
{ |
| 318 |
0 |
col = ColorUtils.getAWTColorFromName(colour); |
| 319 |
|
} |
| 320 |
|
|
| 321 |
0 |
if (col == null) |
| 322 |
|
{ |
| 323 |
0 |
try |
| 324 |
|
{ |
| 325 |
0 |
String[] tokens = colour.split(","); |
| 326 |
0 |
if (tokens.length == 3) |
| 327 |
|
{ |
| 328 |
0 |
int r = Integer.parseInt(tokens[0].trim()); |
| 329 |
0 |
int g = Integer.parseInt(tokens[1].trim()); |
| 330 |
0 |
int b = Integer.parseInt(tokens[2].trim()); |
| 331 |
0 |
col = new Color(r, g, b); |
| 332 |
|
} |
| 333 |
|
} catch (IllegalArgumentException ex) |
| 334 |
|
|
| 335 |
|
{ |
| 336 |
|
|
| 337 |
|
} |
| 338 |
|
} |
| 339 |
|
|
| 340 |
0 |
return col; |
| 341 |
|
} |
| 342 |
|
|
| 343 |
|
|
| 344 |
|
|
| 345 |
|
|
| 346 |
|
|
| 347 |
|
|
| 348 |
|
@param |
| 349 |
|
@return |
| 350 |
|
|
| |
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 5 |
Complexity Density: 0.26 |
|
| 351 |
0 |
public static Color createColourFromName(String name)... |
| 352 |
|
{ |
| 353 |
0 |
if (name == null) |
| 354 |
|
{ |
| 355 |
0 |
return Color.white; |
| 356 |
|
} |
| 357 |
0 |
if (myColours.containsKey(name)) |
| 358 |
|
{ |
| 359 |
0 |
return myColours.get(name); |
| 360 |
|
} |
| 361 |
0 |
int lsize = name.length(); |
| 362 |
0 |
int start = 0; |
| 363 |
0 |
int end = lsize / 3; |
| 364 |
|
|
| 365 |
0 |
int rgbOffset = Math.abs(name.hashCode() % 10) * 15; |
| 366 |
|
|
| 367 |
|
|
| 368 |
|
|
| 369 |
|
|
| 370 |
0 |
int r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) |
| 371 |
|
% 210 + 20; |
| 372 |
0 |
start = end; |
| 373 |
0 |
end += lsize / 3; |
| 374 |
0 |
if (end > lsize) |
| 375 |
|
{ |
| 376 |
0 |
end = lsize; |
| 377 |
|
} |
| 378 |
|
|
| 379 |
|
|
| 380 |
|
|
| 381 |
|
|
| 382 |
0 |
int g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) |
| 383 |
|
% 210 + 20; |
| 384 |
|
|
| 385 |
|
|
| 386 |
|
|
| 387 |
|
|
| 388 |
0 |
int b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20; |
| 389 |
|
|
| 390 |
0 |
Color color = new Color(r, g, b); |
| 391 |
|
|
| 392 |
0 |
if (myColours.size() < MAX_CACHE_SIZE) |
| 393 |
|
{ |
| 394 |
0 |
myColours.put(name, color); |
| 395 |
|
} |
| 396 |
|
|
| 397 |
0 |
return color; |
| 398 |
|
} |
| 399 |
|
|
| 400 |
|
|
| 401 |
|
|
| 402 |
|
|
| 403 |
|
|
| 404 |
|
@param |
| 405 |
|
@return |
| 406 |
|
|
| |
|
| 0% |
Uncovered Elements: 47 (47) |
Complexity: 15 |
Complexity Density: 0.33 |
|
| 407 |
0 |
public static Color getAWTColorFromName(String name)... |
| 408 |
|
{ |
| 409 |
0 |
if (name == null) |
| 410 |
|
{ |
| 411 |
0 |
return null; |
| 412 |
|
} |
| 413 |
0 |
Color col = null; |
| 414 |
0 |
name = name.toLowerCase(Locale.ROOT); |
| 415 |
|
|
| 416 |
|
|
| 417 |
0 |
switch (name) |
| 418 |
|
{ |
| 419 |
0 |
case "black": |
| 420 |
0 |
col = Color.black; |
| 421 |
0 |
break; |
| 422 |
0 |
case "blue": |
| 423 |
0 |
col = Color.blue; |
| 424 |
0 |
break; |
| 425 |
0 |
case "cyan": |
| 426 |
0 |
col = Color.cyan; |
| 427 |
0 |
break; |
| 428 |
0 |
case "darkgray": |
| 429 |
0 |
col = Color.darkGray; |
| 430 |
0 |
break; |
| 431 |
0 |
case "gray": |
| 432 |
0 |
col = Color.gray; |
| 433 |
0 |
break; |
| 434 |
0 |
case "green": |
| 435 |
0 |
col = Color.green; |
| 436 |
0 |
break; |
| 437 |
0 |
case "lightgray": |
| 438 |
0 |
col = Color.lightGray; |
| 439 |
0 |
break; |
| 440 |
0 |
case "magenta": |
| 441 |
0 |
col = Color.magenta; |
| 442 |
0 |
break; |
| 443 |
0 |
case "orange": |
| 444 |
0 |
col = Color.orange; |
| 445 |
0 |
break; |
| 446 |
0 |
case "pink": |
| 447 |
0 |
col = Color.pink; |
| 448 |
0 |
break; |
| 449 |
0 |
case "red": |
| 450 |
0 |
col = Color.red; |
| 451 |
0 |
break; |
| 452 |
0 |
case "white": |
| 453 |
0 |
col = Color.white; |
| 454 |
0 |
break; |
| 455 |
0 |
case "yellow": |
| 456 |
0 |
col = Color.yellow; |
| 457 |
0 |
break; |
| 458 |
|
} |
| 459 |
|
|
| 460 |
0 |
return col; |
| 461 |
|
} |
| 462 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 463 |
|
public enum ColourScheme |
| 464 |
|
{ |
| 465 |
|
NONE, AVOID_RED, AVOID_GREEN, AVOID_BLUE, SATURATED, MEDIUM_SATURATION, DESATURATED, |
| 466 |
|
GREYISH, GREYSCALE, BRIGHT, MEDIUM, DARK |
| 467 |
|
} |
| 468 |
|
|
| |
|
| 0% |
Uncovered Elements: 60 (60) |
Complexity: 14 |
Complexity Density: 0.24 |
|
| 469 |
0 |
public static float[] getHSBRanges(String colourScheme)... |
| 470 |
|
{ |
| 471 |
0 |
float Hmin = 0.0f; |
| 472 |
0 |
float Hmax = 1.0f; |
| 473 |
0 |
float Smin = 0.6f; |
| 474 |
0 |
float Smax = 1.0f; |
| 475 |
0 |
float Bmin = 0.6f; |
| 476 |
0 |
float Bmax = 1.0f; |
| 477 |
0 |
if (!colourScheme.contains(ColourScheme.NONE.name())) |
| 478 |
|
{ |
| 479 |
0 |
for (String scheme : colourScheme.split(",")) |
| 480 |
|
{ |
| 481 |
0 |
Console.debug("Applying colourScheme component " + scheme); |
| 482 |
0 |
ColourScheme cs; |
| 483 |
0 |
try |
| 484 |
|
{ |
| 485 |
0 |
cs = ColourScheme.valueOf(scheme); |
| 486 |
|
} catch (IllegalArgumentException | NullPointerException e) |
| 487 |
|
{ |
| 488 |
0 |
Console.warn("Did not recognise something in the colour scheme '" |
| 489 |
|
+ colourScheme + "'"); |
| 490 |
0 |
return new float[] { Hmin, Hmax, Smin, Smax, Bmin, Bmax }; |
| 491 |
|
} |
| 492 |
0 |
switch (cs) |
| 493 |
|
{ |
| 494 |
0 |
case AVOID_RED: |
| 495 |
0 |
Hmin = 0.15f; |
| 496 |
0 |
Hmax = 0.85f; |
| 497 |
0 |
break; |
| 498 |
0 |
case AVOID_GREEN: |
| 499 |
0 |
Hmin = 0.48f; |
| 500 |
0 |
Hmax = 0.18f; |
| 501 |
0 |
break; |
| 502 |
0 |
case AVOID_BLUE: |
| 503 |
0 |
Hmin = 0.81f; |
| 504 |
0 |
Hmax = 0.51f; |
| 505 |
0 |
break; |
| 506 |
0 |
case SATURATED: |
| 507 |
0 |
Smin = 1.0f; |
| 508 |
0 |
Smax = 1.0f; |
| 509 |
0 |
break; |
| 510 |
0 |
case DESATURATED: |
| 511 |
0 |
Smin = 0.2f; |
| 512 |
0 |
Smax = 0.6f; |
| 513 |
0 |
break; |
| 514 |
0 |
case GREYISH: |
| 515 |
0 |
Smin = 0.0f; |
| 516 |
0 |
Smax = 0.2f; |
| 517 |
0 |
case GREYSCALE: |
| 518 |
0 |
Smin = 0.0f; |
| 519 |
0 |
Smax = 0.0f; |
| 520 |
0 |
Bmin = 0.1f; |
| 521 |
0 |
Bmax = 0.9f; |
| 522 |
0 |
break; |
| 523 |
0 |
case BRIGHT: |
| 524 |
0 |
Bmin = 1.0f; |
| 525 |
0 |
Bmax = 1.0f; |
| 526 |
0 |
break; |
| 527 |
0 |
case MEDIUM: |
| 528 |
0 |
Bmin = 0.6f; |
| 529 |
0 |
Bmax = 0.8f; |
| 530 |
0 |
case DARK: |
| 531 |
0 |
Bmin = 0.1f; |
| 532 |
0 |
Bmax = 0.4f; |
| 533 |
0 |
break; |
| 534 |
0 |
case NONE: |
| 535 |
0 |
break; |
| 536 |
|
} |
| 537 |
|
} |
| 538 |
|
} |
| 539 |
0 |
return new float[] { Hmin, Hmax, Smin, Smax, Bmin, Bmax }; |
| 540 |
|
} |
| 541 |
|
|
| 542 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 543 |
0 |
private static String getHSBColourSpacedCacheKey(String name,... |
| 544 |
|
String colourScheme) |
| 545 |
|
{ |
| 546 |
0 |
return name.hashCode() + "::" + colourScheme; |
| 547 |
|
} |
| 548 |
|
|
| 549 |
|
|
| 550 |
|
|
| 551 |
|
|
| 552 |
|
|
| 553 |
|
@param |
| 554 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 555 |
0 |
public static void restoreMyHSBSpacedColours(... |
| 556 |
|
Map<String, Color> colorMap) |
| 557 |
|
{ |
| 558 |
0 |
for(Entry<String, Color> entry : colorMap.entrySet()) { |
| 559 |
0 |
String cacheKey = getHSBColourSpacedCacheKey(entry.getKey(), "NONE"); |
| 560 |
0 |
ColorUtils.myHSBSpacedColours.put(cacheKey, entry.getValue()); |
| 561 |
|
} |
| 562 |
|
} |
| 563 |
|
|
| 564 |
|
|
| |
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 5 |
Complexity Density: 0.29 |
|
| 565 |
0 |
public static Color getColourFromNameAndScheme(String name,... |
| 566 |
|
String colourScheme) |
| 567 |
|
{ |
| 568 |
0 |
String cacheKey = getHSBColourSpacedCacheKey(name, colourScheme); |
| 569 |
0 |
if (myHSBSpacedColours.containsKey(cacheKey)) |
| 570 |
|
{ |
| 571 |
0 |
return myHSBSpacedColours.get(cacheKey); |
| 572 |
|
} |
| 573 |
0 |
float[] vals = getHSBRanges(colourScheme); |
| 574 |
0 |
Color col = null; |
| 575 |
0 |
if (vals.length > 5) |
| 576 |
|
{ |
| 577 |
0 |
col = getHSBColourspacedColourFromName(name, vals[0], vals[1], |
| 578 |
|
vals[2], vals[3], vals[4], vals[5]); |
| 579 |
|
|
| 580 |
|
|
| 581 |
|
|
| 582 |
0 |
int iterationCounter = 0; |
| 583 |
0 |
HSB_DISTANCE_THRESHOLD = DEFAULT_HSB_DISTANCE_THRESHOLD; |
| 584 |
|
|
| 585 |
|
|
| 586 |
|
|
| 587 |
0 |
while (!isColorDistinct(col)) |
| 588 |
|
{ |
| 589 |
0 |
col = adjustColor(col); |
| 590 |
|
|
| 591 |
0 |
iterationCounter++; |
| 592 |
|
|
| 593 |
|
|
| 594 |
|
|
| 595 |
0 |
if (iterationCounter > MAX_ITERATION_FOR_ADJUSTING_COLOR) |
| 596 |
|
{ |
| 597 |
0 |
HSB_DISTANCE_THRESHOLD = HSB_DISTANCE_THRESHOLD |
| 598 |
|
- DECREMENT_VAL_FOR_HSB_DISTANCE_THRESHOLD; |
| 599 |
0 |
iterationCounter = 0; |
| 600 |
|
} |
| 601 |
|
} |
| 602 |
|
|
| 603 |
0 |
myHSBSpacedColours.put(cacheKey, col); |
| 604 |
|
} |
| 605 |
0 |
return col; |
| 606 |
|
} |
| 607 |
|
|
| |
|
| 0% |
Uncovered Elements: 42 (42) |
Complexity: 11 |
Complexity Density: 0.5 |
|
| 608 |
0 |
public static Color getHSBColourspacedColourFromName(String name,... |
| 609 |
|
float Hmin, float Hmax, float Smin, float Smax, float Bmin, |
| 610 |
|
float Bmax) |
| 611 |
|
{ |
| 612 |
0 |
if (name == null) |
| 613 |
|
{ |
| 614 |
0 |
return Color.white; |
| 615 |
|
} |
| 616 |
|
|
| 617 |
|
|
| 618 |
|
|
| 619 |
0 |
byte[] hash = StringUtils.getHashedBytes(name); |
| 620 |
0 |
int b = hash.length > 0 ? Byte.toUnsignedInt(hash[0]) : 0; |
| 621 |
0 |
int g = hash.length > 1 ? Byte.toUnsignedInt(hash[1]) : 0; |
| 622 |
0 |
int r = hash.length > 2 ? Byte.toUnsignedInt(hash[2]) : 0; |
| 623 |
|
|
| 624 |
0 |
float[] hsbf = Color.RGBtoHSB(r, g, b, null); |
| 625 |
|
|
| 626 |
0 |
if (hsbf.length < 3) |
| 627 |
|
{ |
| 628 |
|
|
| 629 |
0 |
Console.warn("Unexpected short length of HSB float array"); |
| 630 |
|
} |
| 631 |
0 |
float h0 = hsbf.length > 0 ? hsbf[0] : 0f; |
| 632 |
0 |
float s0 = hsbf.length > 1 ? hsbf[1] : 0f; |
| 633 |
0 |
float b0 = hsbf.length > 2 ? hsbf[2] : 0f; |
| 634 |
|
|
| 635 |
|
|
| 636 |
|
|
| 637 |
|
|
| 638 |
|
|
| 639 |
0 |
float h1 = 0f; |
| 640 |
0 |
if (Hmin > Hmax) |
| 641 |
|
{ |
| 642 |
0 |
Hmax += 1f; |
| 643 |
|
} |
| 644 |
0 |
h1 = Hmin + (Hmax - Hmin) * h0; |
| 645 |
0 |
if (h1 > 1f) |
| 646 |
|
{ |
| 647 |
0 |
h1 -= 1f; |
| 648 |
|
} |
| 649 |
0 |
float s1 = Smin + (Smax - Smin) * s0; |
| 650 |
0 |
float b1 = Bmin + (Bmax - Bmin) * b0; |
| 651 |
|
|
| 652 |
0 |
Console.debug("Setting new colour for '" + name + "' with H=" + h1 |
| 653 |
|
+ ", S=" + s1 + ", B=" + b1); |
| 654 |
|
|
| 655 |
0 |
return Color.getHSBColor(h1, s1, b1); |
| 656 |
|
} |
| 657 |
|
|
| 658 |
|
|
| 659 |
|
|
| 660 |
|
|
| 661 |
|
|
| 662 |
|
@param |
| 663 |
|
|
| 664 |
|
@return |
| 665 |
|
|
| |
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 666 |
0 |
private static boolean isColorDistinct(Color newColor)... |
| 667 |
|
{ |
| 668 |
0 |
float[] newHSB = convertRGBColorToHSB(newColor); |
| 669 |
|
|
| 670 |
0 |
for (Color existingColor : myHSBSpacedColours.values()) |
| 671 |
|
{ |
| 672 |
0 |
float[] existingHSB = convertRGBColorToHSB(existingColor); |
| 673 |
0 |
float distanceFromExistingColor = hsbDistance(newHSB, existingHSB); |
| 674 |
|
|
| 675 |
0 |
if (distanceFromExistingColor < HSB_DISTANCE_THRESHOLD) |
| 676 |
|
{ |
| 677 |
0 |
return false; |
| 678 |
|
} |
| 679 |
|
} |
| 680 |
0 |
return true; |
| 681 |
|
} |
| 682 |
|
|
| 683 |
|
|
| 684 |
|
|
| 685 |
|
|
| 686 |
|
|
| 687 |
|
@param |
| 688 |
|
|
| 689 |
|
@return |
| 690 |
|
|
| |
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 691 |
0 |
private static Color adjustColor(Color col) {... |
| 692 |
0 |
float[] hsbComponents = convertRGBColorToHSB(col); |
| 693 |
0 |
hsbComponents[POS_HUE] = (float) ((hsbComponents[POS_HUE] |
| 694 |
|
+ GOLDEN_RATIO_CONJUGATE) % 1.0); |
| 695 |
|
|
| 696 |
|
|
| 697 |
0 |
if (hsbComponents[POS_HUE] > 1f) |
| 698 |
|
{ |
| 699 |
0 |
hsbComponents[POS_HUE] -= 1f; |
| 700 |
|
} |
| 701 |
|
|
| 702 |
|
|
| 703 |
|
|
| 704 |
0 |
hsbComponents[POS_SATURATION] = 0.4f |
| 705 |
|
+ (hsbComponents[POS_HUE] * 0.5f); |
| 706 |
|
|
| 707 |
|
|
| 708 |
0 |
hsbComponents[POS_BRIGHTNESS] = 0.5f |
| 709 |
|
+ (hsbComponents[POS_HUE] * 0.4f); |
| 710 |
|
|
| 711 |
0 |
Color adjustedColor = Color.getHSBColor((float) hsbComponents[POS_HUE], |
| 712 |
|
hsbComponents[POS_SATURATION], |
| 713 |
|
hsbComponents[POS_BRIGHTNESS]); |
| 714 |
|
|
| 715 |
0 |
return adjustedColor; |
| 716 |
|
} |
| 717 |
|
|
| 718 |
|
|
| 719 |
|
|
| 720 |
|
|
| 721 |
|
|
| 722 |
|
@param |
| 723 |
|
|
| 724 |
|
@return |
| 725 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 726 |
0 |
private static float[] convertRGBColorToHSB(Color color)... |
| 727 |
|
{ |
| 728 |
0 |
float[] hsb = new float[3]; |
| 729 |
0 |
Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb); |
| 730 |
0 |
return hsb; |
| 731 |
|
} |
| 732 |
|
|
| 733 |
|
|
| 734 |
|
|
| 735 |
|
|
| 736 |
|
|
| 737 |
|
@param |
| 738 |
|
|
| 739 |
|
@param |
| 740 |
|
|
| 741 |
|
@return |
| 742 |
|
|
| |
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 743 |
0 |
private static float hsbDistance(float[] hsbX, float[] hsbY)... |
| 744 |
|
{ |
| 745 |
|
|
| 746 |
0 |
float diffH = Math.abs(hsbX[POS_HUE] - hsbY[POS_HUE]); |
| 747 |
0 |
if (diffH > 0.5) |
| 748 |
|
{ |
| 749 |
0 |
diffH = 1.0f - diffH; |
| 750 |
|
} |
| 751 |
0 |
float diffS = hsbX[POS_SATURATION] - hsbY[POS_SATURATION]; |
| 752 |
0 |
float diffB = hsbX[POS_BRIGHTNESS] - hsbY[POS_BRIGHTNESS]; |
| 753 |
|
|
| 754 |
|
|
| 755 |
0 |
return (float) Math.sqrt(diffH * diffH + diffS * diffS + diffB * diffB); |
| 756 |
|
} |
| 757 |
|
|
| 758 |
|
} |