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.

ColourManagerImpl.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2015 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.util.colours.Colour;
  25. import com.dmdirc.util.validators.ColourValidator;
  26. import com.dmdirc.util.validators.Validator;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import static com.dmdirc.util.LogUtils.USER_ERROR;
  32. /**
  33. * The colour manager manages the colour scheme for the IRC client. It allows other components to
  34. * use IRC colour codes instead of absolute colours.
  35. */
  36. public class ColourManagerImpl implements ColourManager {
  37. private static final Logger LOG = LoggerFactory.getLogger(ColourManagerImpl.class);
  38. /** Default colours used for the standard 16 IRC colours. */
  39. private static final Colour[] DEFAULT_COLOURS = {
  40. Colour.WHITE, Colour.BLACK, new Colour(0, 0, 127), new Colour(0, 141, 0),
  41. Colour.RED, new Colour(127, 0, 0), new Colour(160, 15, 160), new Colour(252, 127, 0),
  42. Colour.YELLOW, new Colour(0, 252, 0), new Colour(0, 128, 128), new Colour(0, 255, 255),
  43. Colour.BLUE, new Colour(255, 0, 255), Colour.GRAY, Colour.LIGHT_GRAY,};
  44. /** Colour cache. */
  45. private final Map<String, Colour> colourCache = new HashMap<>();
  46. /** Config manager to read settings from. */
  47. private final AggregateConfigProvider configManager;
  48. /** Actual colours we're using for the 16 IRC colours. */
  49. private final Colour[] ircColours = DEFAULT_COLOURS.clone();
  50. /**
  51. * Creates a new instance of {@link ColourManagerImpl}.
  52. *
  53. * @param configManager The manager to read config settings from.
  54. */
  55. public ColourManagerImpl(final AggregateConfigProvider configManager) {
  56. this.configManager = configManager;
  57. configManager.addChangeListener("colour", (domain, key) -> initColours());
  58. initColours();
  59. }
  60. /**
  61. * Initialises the IRC_COLOURS array.
  62. */
  63. private void initColours() {
  64. final Validator<String> validator = new ColourValidator();
  65. for (int i = 0; i < 16; i++) {
  66. if (configManager.hasOptionString("colour", String.valueOf(i), validator)) {
  67. ircColours[i] = getColourFromHex(
  68. configManager.getOptionString("colour", String.valueOf(i)));
  69. colourCache.remove(String.valueOf(i));
  70. } else if (!ircColours[i].equals(DEFAULT_COLOURS[i])) {
  71. ircColours[i] = DEFAULT_COLOURS[i];
  72. colourCache.remove(String.valueOf(i));
  73. }
  74. }
  75. }
  76. @Override
  77. public Colour getColourFromString(final String spec, final Colour fallback) {
  78. if (colourCache.containsKey(spec)) {
  79. return colourCache.get(spec);
  80. }
  81. Colour res = null;
  82. if (spec != null) {
  83. if (spec.length() < 3) {
  84. int num;
  85. try {
  86. num = Integer.parseInt(spec);
  87. } catch (NumberFormatException ex) {
  88. num = -1;
  89. }
  90. if (num >= 0 && num <= 15) {
  91. res = getColourFromIrcCode(num);
  92. }
  93. } else if (spec.length() == 6) {
  94. res = getColourFromHex(spec);
  95. }
  96. }
  97. if (res == null) {
  98. LOG.warn(USER_ERROR, "Invalid colour format: {}", spec);
  99. res = fallback;
  100. } else {
  101. colourCache.put(spec, res);
  102. }
  103. return res;
  104. }
  105. @Override
  106. public Colour getColourFromHex(final String hex) {
  107. if (colourCache.containsKey(hex)) {
  108. return colourCache.get(hex);
  109. }
  110. if (hex.length() < 6) {
  111. LOG.warn(USER_ERROR, "Invalid colour: #{}", hex);
  112. return Colour.WHITE;
  113. }
  114. final Colour colour;
  115. try {
  116. colour = new Colour(
  117. Integer.parseInt(hex.substring(0, 2), 16),
  118. Integer.parseInt(hex.substring(2, 4), 16),
  119. Integer.parseInt(hex.substring(4, 6), 16));
  120. } catch (NumberFormatException ex) {
  121. LOG.warn(USER_ERROR, "Invalid colour: #{}", hex);
  122. return Colour.WHITE;
  123. }
  124. colourCache.put(hex, colour);
  125. return colour;
  126. }
  127. @Override
  128. public Colour getColourFromIrcCode(final int number) {
  129. if (number >= 0 && number <= 15) {
  130. return ircColours[number];
  131. } else {
  132. LOG.warn(USER_ERROR, "Invalid colour: {}", number);
  133. return Colour.WHITE;
  134. }
  135. }
  136. }