Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ColourManagerImpl.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.ui.messages;
  18. import com.dmdirc.config.provider.AggregateConfigProvider;
  19. import com.dmdirc.util.colours.Colour;
  20. import com.dmdirc.util.validators.ColourValidator;
  21. import com.dmdirc.util.validators.Validator;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import static com.dmdirc.util.LogUtils.USER_ERROR;
  27. /**
  28. * The colour manager manages the colour scheme for the IRC client. It allows other components to
  29. * use IRC colour codes instead of absolute colours.
  30. */
  31. public class ColourManagerImpl implements ColourManager {
  32. private static final Logger LOG = LoggerFactory.getLogger(ColourManagerImpl.class);
  33. /** Default colours used for the standard 16 IRC colours. */
  34. private static final Colour[] DEFAULT_COLOURS = {
  35. Colour.WHITE, Colour.BLACK, new Colour(0, 0, 127), new Colour(0, 141, 0),
  36. Colour.RED, new Colour(127, 0, 0), new Colour(160, 15, 160), new Colour(252, 127, 0),
  37. Colour.YELLOW, new Colour(0, 252, 0), new Colour(0, 128, 128), new Colour(0, 255, 255),
  38. Colour.BLUE, new Colour(255, 0, 255), Colour.GRAY, Colour.LIGHT_GRAY,};
  39. /** Colour cache. */
  40. private final Map<String, Colour> colourCache = new HashMap<>();
  41. /** Config manager to read settings from. */
  42. private final AggregateConfigProvider configManager;
  43. /** Actual colours we're using for the 16 IRC colours. */
  44. private final Colour[] ircColours = DEFAULT_COLOURS.clone();
  45. /**
  46. * Creates a new instance of {@link ColourManagerImpl}.
  47. *
  48. * @param configManager The manager to read config settings from.
  49. */
  50. public ColourManagerImpl(final AggregateConfigProvider configManager) {
  51. this.configManager = configManager;
  52. configManager.addChangeListener("colour", (domain, key) -> initColours());
  53. initColours();
  54. }
  55. /**
  56. * Initialises the IRC_COLOURS array.
  57. */
  58. private void initColours() {
  59. final Validator<String> validator = new ColourValidator();
  60. for (int i = 0; i < 16; i++) {
  61. if (configManager.hasOptionString("colour", String.valueOf(i), validator)) {
  62. ircColours[i] = getColourFromHex(
  63. configManager.getOptionString("colour", String.valueOf(i)));
  64. colourCache.remove(String.valueOf(i));
  65. } else if (!ircColours[i].equals(DEFAULT_COLOURS[i])) {
  66. ircColours[i] = DEFAULT_COLOURS[i];
  67. colourCache.remove(String.valueOf(i));
  68. }
  69. }
  70. }
  71. @Override
  72. public Colour getColourFromString(final String spec, final Colour fallback) {
  73. if (colourCache.containsKey(spec)) {
  74. return colourCache.get(spec);
  75. }
  76. Colour res = null;
  77. if (spec != null) {
  78. if (spec.length() < 3) {
  79. int num;
  80. try {
  81. num = Integer.parseInt(spec);
  82. } catch (NumberFormatException ex) {
  83. num = -1;
  84. }
  85. if (num >= 0 && num <= 15) {
  86. res = getColourFromIrcCode(num);
  87. }
  88. } else if (spec.length() == 6) {
  89. res = getColourFromHex(spec);
  90. }
  91. }
  92. if (res == null) {
  93. LOG.warn(USER_ERROR, "Invalid colour format: {}", spec);
  94. res = fallback;
  95. } else {
  96. colourCache.put(spec, res);
  97. }
  98. return res;
  99. }
  100. @Override
  101. public Colour getColourFromHex(final String hex) {
  102. if (colourCache.containsKey(hex)) {
  103. return colourCache.get(hex);
  104. }
  105. if (hex.length() < 6) {
  106. LOG.warn(USER_ERROR, "Invalid colour: #{}", hex);
  107. return Colour.WHITE;
  108. }
  109. final Colour colour;
  110. try {
  111. colour = new Colour(
  112. Integer.parseInt(hex.substring(0, 2), 16),
  113. Integer.parseInt(hex.substring(2, 4), 16),
  114. Integer.parseInt(hex.substring(4, 6), 16));
  115. } catch (NumberFormatException ex) {
  116. LOG.warn(USER_ERROR, "Invalid colour: #{}", hex);
  117. return Colour.WHITE;
  118. }
  119. colourCache.put(hex, colour);
  120. return colour;
  121. }
  122. @Override
  123. public Colour getColourFromIrcCode(final int number) {
  124. if (number >= 0 && number <= 15) {
  125. return ircColours[number];
  126. } else {
  127. LOG.warn(USER_ERROR, "Invalid colour: {}", number);
  128. return Colour.WHITE;
  129. }
  130. }
  131. }