Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ColourManager.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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 com.dmdirc.interfaces.config.AggregateConfigProvider;
  24. import com.dmdirc.interfaces.config.ConfigChangeListener;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import com.dmdirc.logger.Logger;
  27. import com.dmdirc.util.colours.Colour;
  28. import com.dmdirc.util.validators.ColourValidator;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. /**
  32. * The colour manager manages the colour scheme for the IRC client. It allows other components to
  33. * use IRC colour codes instead of absolute colours.
  34. */
  35. public class ColourManager {
  36. /** Default colours used for the standard 16 IRC colours. */
  37. private static final Colour[] DEFAULT_COLOURS = {
  38. Colour.WHITE, Colour.BLACK, new Colour(0, 0, 127), new Colour(0, 141, 0),
  39. Colour.RED, new Colour(127, 0, 0), new Colour(160, 15, 160), new Colour(252, 127, 0),
  40. Colour.YELLOW, new Colour(0, 252, 0), new Colour(0, 128, 128), new Colour(0, 255, 255),
  41. Colour.BLUE, new Colour(255, 0, 255), Colour.GRAY, Colour.LIGHT_GRAY,};
  42. /** Colour cache. */
  43. private final Map<String, Colour> colourCache = new HashMap<>();
  44. /** Config manager to read settings from. */
  45. private final AggregateConfigProvider configManager;
  46. /** Actual colours we're using for the 16 IRC colours. */
  47. private final Colour[] ircColours = DEFAULT_COLOURS.clone();
  48. /**
  49. * Creates a new instance of {@link ColourManager}.
  50. *
  51. * @param configManager The manager to read config settings from.
  52. */
  53. public ColourManager(final AggregateConfigProvider configManager) {
  54. this.configManager = configManager;
  55. configManager.addChangeListener("colour", new ConfigChangeListener() {
  56. @Override
  57. public void configChanged(final String domain, final String key) {
  58. initColours();
  59. }
  60. });
  61. initColours();
  62. }
  63. /**
  64. * Initialises the IRC_COLOURS array.
  65. */
  66. private void initColours() {
  67. final ColourValidator validator = new ColourValidator();
  68. for (int i = 0; i < 16; i++) {
  69. if (configManager.hasOptionString("colour", String.valueOf(i), validator)) {
  70. ircColours[i] = getColourFromHex(
  71. configManager.getOptionString("colour", String.valueOf(i)));
  72. colourCache.remove(String.valueOf(i));
  73. } else if (!ircColours[i].equals(DEFAULT_COLOURS[i])) {
  74. ircColours[i] = DEFAULT_COLOURS[i];
  75. colourCache.remove(String.valueOf(i));
  76. }
  77. }
  78. }
  79. /**
  80. * Parses either a 1-2 digit IRC colour, or a 6 digit hex colour from the target string, and
  81. * returns the corresponding colour. Returns the specified fallback colour if the spec can't be
  82. * parsed.
  83. *
  84. * @param spec The string to parse
  85. * @param fallback The colour to use if the spec isn't valid
  86. *
  87. * @return A colour representation of the specified string
  88. */
  89. public Colour getColourFromString(final String spec, final Colour fallback) {
  90. if (colourCache.containsKey(spec)) {
  91. return colourCache.get(spec);
  92. }
  93. Colour res = null;
  94. if (spec != null) {
  95. if (spec.length() < 3) {
  96. int num;
  97. try {
  98. num = Integer.parseInt(spec);
  99. } catch (NumberFormatException ex) {
  100. num = -1;
  101. }
  102. if (num >= 0 && num <= 15) {
  103. res = getColourFromIrcCode(num);
  104. }
  105. } else if (spec.length() == 6) {
  106. res = getColourFromHex(spec);
  107. }
  108. }
  109. if (res == null) {
  110. Logger.userError(ErrorLevel.MEDIUM, "Invalid colour format: " + spec);
  111. res = fallback;
  112. } else {
  113. colourCache.put(spec, res);
  114. }
  115. return res;
  116. }
  117. /**
  118. * Returns a Colour object that corresponds to the specified 6-digit hex string. If the string
  119. * is invalid, logs a warning and returns white.
  120. *
  121. * @param hex The hex string to convert into a Colour
  122. *
  123. * @return A Colour object corresponding to the hex input
  124. */
  125. public Colour getColourFromHex(final String hex) {
  126. if (colourCache.containsKey(hex)) {
  127. return colourCache.get(hex);
  128. }
  129. if (hex.length() < 6) {
  130. Logger.userError(ErrorLevel.MEDIUM, "Invalid colour #" + hex);
  131. return Colour.WHITE;
  132. }
  133. final Colour colour;
  134. try {
  135. colour = new Colour(
  136. Integer.parseInt(hex.substring(0, 2), 16),
  137. Integer.parseInt(hex.substring(2, 4), 16),
  138. Integer.parseInt(hex.substring(4, 6), 16));
  139. } catch (NumberFormatException ex) {
  140. Logger.userError(ErrorLevel.MEDIUM, "Invalid colour #" + hex);
  141. return Colour.WHITE;
  142. }
  143. colourCache.put(hex, colour);
  144. return colour;
  145. }
  146. /**
  147. * Returns a Colour object that represents the colour associated with the specified IRC colour
  148. * code. If the code is not found, a warning is logged with the client's Logger class, and white
  149. * is returned.
  150. *
  151. * @param number The IRC colour code to look up
  152. *
  153. * @return The corresponding Colour object
  154. */
  155. public Colour getColourFromIrcCode(final int number) {
  156. if (number >= 0 && number <= 15) {
  157. return ircColours[number];
  158. } else {
  159. Logger.userError(ErrorLevel.MEDIUM, "Invalid colour: " + number);
  160. return Colour.WHITE;
  161. }
  162. }
  163. }