You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ColorFactory.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2013 Alex Lewis.
  3. * Copyright 2013 gitblit.com.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.gitblit.utils;
  18. import java.awt.Color;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.Random;
  22. import java.util.Set;
  23. /**
  24. * Factory for creating color maps.
  25. *
  26. * @author Alex Lewis
  27. */
  28. public class ColorFactory {
  29. private static final double MAX_TINT_FACTOR = 1;
  30. private static final double MIN_TINT_FACTOR = 0.2;
  31. private static final double FIXED_TINT_FACTOR = 0.875;
  32. /**
  33. * Builds a map of the supplied keys to a random color tinted according to
  34. * the key's position in the set.
  35. *
  36. * Depending on the number of keys in the set a tint is calculated from 1.0
  37. * (I.e. white) to a minimum tint. The keys are sorted such that the
  38. * "lowest" value will have a full tint applied to it (1.0) with an equally
  39. * decreasing tint applied to each key thereafter.
  40. *
  41. * @param keys
  42. * The keys to create a tinted color for.
  43. * @param baseColor
  44. * the base color (optional)
  45. * @return The map of key to tinted color.
  46. */
  47. public <T> Map<T, String> getGraduatedColorMap(Set<T> keys, Color baseColor) {
  48. Map<T, String> colorMap = new HashMap<T, String>();
  49. if (baseColor == null) {
  50. baseColor = getRandomColor();
  51. }
  52. double tintStep = (MAX_TINT_FACTOR - MIN_TINT_FACTOR) / keys.size();
  53. double currentTint = MAX_TINT_FACTOR;
  54. for (T key : keys) {
  55. Color color = tintColor(baseColor, currentTint);
  56. colorMap.put(key, getColorString(color));
  57. currentTint -= tintStep;
  58. }
  59. return colorMap;
  60. }
  61. /**
  62. * Builds a map of the supplied keys to random colors.
  63. *
  64. * Each color is selected randomly and tinted with a fixed tint.
  65. *
  66. * @param keys The keys to create the mapped colors.
  67. * @return The map of key to random color.
  68. */
  69. public <T> Map<T, String> getRandomColorMap(Set<T> keys) {
  70. Map<T, String> colorMap = new HashMap<T, String>();
  71. for (T key : keys) {
  72. Color color = tintColor(getRandomColor(), FIXED_TINT_FACTOR);
  73. colorMap.put(key, getColorString(color));
  74. }
  75. return colorMap;
  76. }
  77. private Color getRandomColor() {
  78. Random random = new Random();
  79. Color randomColor = new Color(random.nextInt(256), random.nextInt(256),
  80. random.nextInt(256));
  81. return randomColor;
  82. }
  83. private Color tintColor(Color origColor, double tintFactor) {
  84. int tintedRed = applyTint(origColor.getRed(), tintFactor);
  85. int tintedGreen = applyTint(origColor.getGreen(), tintFactor);
  86. int tintedBlue = applyTint(origColor.getBlue(), tintFactor);
  87. Color tintedColor = new Color(tintedRed, tintedGreen, tintedBlue);
  88. return tintedColor;
  89. }
  90. /**
  91. * Convert the color to an HTML compatible color string in hex format E.g.
  92. * #FF0000
  93. *
  94. * @param color The color to convert
  95. * @return The string version of the color I.e. #RRGGBB
  96. */
  97. private String getColorString(Color color) {
  98. return "#" + Integer.toHexString(color.getRGB() & 0x00ffffff);
  99. }
  100. /**
  101. * Tint the supplied color with a tint factor (0 to 1 inclusive) to make the
  102. * colour more pale I.e. closer to white.
  103. *
  104. * A Tint of 0 has no effect, a Tint of 1 turns the color white.
  105. *
  106. * @param color The original color
  107. * @param tintFactor The factor - 0 to 1 inclusive
  108. * @return The tinted color.
  109. */
  110. private int applyTint(int color, double tintFactor) {
  111. return (int) (color + ((255 - color) * tintFactor));
  112. }
  113. }