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.

ColourManager.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.messages;
  23. import java.awt.Color;
  24. import com.dmdirc.logger.ErrorLevel;
  25. import com.dmdirc.logger.Logger;
  26. /**
  27. * The colour manager manages the colour scheme for the IRC client. It allows
  28. * other components to use IRC colour codes instead of absolute colours.
  29. * @author chris
  30. */
  31. public final class ColourManager {
  32. /** Creates a new instance of ColourManager. */
  33. private ColourManager() {
  34. }
  35. /**
  36. * Parses either a 1-2 digit IRC colour, or a 6 digit hex colour from the
  37. * target string, and returns the corresponding colour. Returns the
  38. * specified fallback colour if the spec can't be parsed.
  39. * @param spec The string to parse
  40. * @param fallback The colour to use if the spec isn't valid
  41. * @return A colour representation of the specified string
  42. */
  43. public static Color parseColour(final String spec, final Color fallback) {
  44. Color res = null;
  45. if (spec.length() < 3) {
  46. int num;
  47. try {
  48. num = Integer.parseInt(spec);
  49. } catch (NumberFormatException ex) {
  50. num = -1;
  51. }
  52. if (num >= 0 && num <= 15) {
  53. res = getColour(num);
  54. }
  55. } else if (spec.length() == 6) {
  56. res = getColour(spec);
  57. }
  58. if (res == null) {
  59. Logger.error(ErrorLevel.WARNING, "Invalid colour format: " + spec);
  60. res = fallback;
  61. }
  62. return res;
  63. }
  64. /**
  65. * Parses either a 1-2 digit IRC colour, or a 6 digit hex colour from the
  66. * target string, and returns the corresponding colour. Returns white if the
  67. * spec can't be parsed.
  68. * @param spec The string to parse
  69. * @return A colour representation of the specified string
  70. */
  71. public static Color parseColour(final String spec) {
  72. return parseColour(spec, Color.WHITE);
  73. }
  74. /**
  75. * Returns a Color object that corresponds to the specified 6-digit hex
  76. * string. If the string is invalid, logs a warning and returns white.
  77. * @param hex The hex string to convert into a Color
  78. * @return A Color object corresponding to the hex input
  79. */
  80. public static Color getColour(final String hex) {
  81. try {
  82. return Color.decode("#" + hex);
  83. } catch (NumberFormatException ex) {
  84. Logger.error(ErrorLevel.WARNING, "Invalid colour #" + hex, ex);
  85. return Color.WHITE;
  86. }
  87. }
  88. /**
  89. * Returns a Color object that represents the colour associated with the
  90. * specified IRC colour code. If the code is not found, a warning is logged
  91. * with the client's Logger class, and white is returned.
  92. * @param number The IRC colour code to look up
  93. * @return The corresponding Color object
  94. */
  95. public static Color getColour(final int number) {
  96. switch (number) {
  97. case 0:
  98. return Color.WHITE;
  99. case 1:
  100. return Color.BLACK;
  101. case 2:
  102. return new Color(0, 0, 127);
  103. case 3:
  104. return new Color(0, 141, 0);
  105. case 4:
  106. return Color.RED;
  107. case 5:
  108. return new Color(127, 0, 0);
  109. case 6:
  110. return new Color(160, 15, 160);
  111. case 7:
  112. return new Color(252, 127, 0);
  113. case 8:
  114. return Color.YELLOW;
  115. case 9:
  116. return new Color(0, 252, 0);
  117. case 10:
  118. return new Color(0, 128, 128);
  119. case 11:
  120. return new Color(0, 255, 255);
  121. case 12:
  122. return Color.BLUE;
  123. case 13:
  124. return new Color(255, 0, 255);
  125. case 14:
  126. return Color.GRAY;
  127. case 15:
  128. return Color.LIGHT_GRAY;
  129. default:
  130. Logger.error(ErrorLevel.WARNING, "Invalid colour: " + number);
  131. return Color.WHITE;
  132. }
  133. }
  134. /**
  135. * Retrieves the hex representation of the specified colour.
  136. * @param colour The colour to be parsed
  137. * @return A 6-digit hex string representing the colour
  138. */
  139. public static String getHex(final Color colour) {
  140. final int r = colour.getRed();
  141. final int g = colour.getGreen();
  142. final int b = colour.getBlue();
  143. return toHex(r) + toHex(g) + toHex(b);
  144. }
  145. /**
  146. * Converts the specified integer (in the range 0-255) into a hex string.
  147. * @param value The integer to convert
  148. * @return A char digit hex string representing the specified integer
  149. */
  150. private static String toHex(final int value) {
  151. final char[] chars = {
  152. '0', '1', '2', '3', '4', '5', '6', '7',
  153. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
  154. };
  155. return ("" + chars[value / 16]) + chars[value % 16];
  156. }
  157. }