1. /*
  2. * @(#)GTKColorType.java 1.6 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.gtk;
  8. import java.awt.Color;
  9. import javax.swing.plaf.ColorUIResource;
  10. /**
  11. * @version 1.6, 01/23/03
  12. * @author Scott Violet
  13. */
  14. class GTKColorType extends ColorType {
  15. // GTK allows you to specify the foreground and background in a
  16. // gtkrc, the rest (dark, mid, light) are calculated from these
  17. // values.
  18. public static final ColorType LIGHT = new GTKColorType("Light");
  19. public static final ColorType DARK = new GTKColorType("Dark");
  20. public static final ColorType MID = new GTKColorType("Mid");
  21. public static final ColorType BLACK = new GTKColorType("Black");
  22. public static final ColorType WHITE = new GTKColorType("White");
  23. public static final int MAX_COUNT;
  24. private static final float[] HLS_COLORS = new float[3];
  25. private static final Object HLS_COLOR_LOCK = new Object();
  26. static {
  27. MAX_COUNT = WHITE.getID() + 1;
  28. }
  29. private static int hlsToRGB(float h, float l, float s) {
  30. float m2 = (l <= .5f) ? (l * (1 + s)) : (l + s - l * s);
  31. float m1 = 2.0f * l - m2;
  32. float r, g, b;
  33. if (s == 0.0) {
  34. if (h == 0.0) {
  35. r = g = b = l;
  36. }
  37. else {
  38. r = g = b = 0;
  39. }
  40. }
  41. else {
  42. r = hlsValue(m1, m2, h + 120);
  43. g = hlsValue(m1, m2, h);
  44. b = hlsValue(m1, m2, h - 120);
  45. }
  46. return (((int)(r * 255)) << 16) | (((int)(g * 255.0)) << 8) |
  47. ((int)(b * 255));
  48. }
  49. private static float hlsValue(float n1, float n2, float h) {
  50. if (h > 360) {
  51. h -= 360;
  52. }
  53. else if (h < 0) {
  54. h += 360;
  55. }
  56. if (h < 60) {
  57. return n1 + (n2 - n1) * h / 60.0f;
  58. }
  59. else if (h < 180) {
  60. return n2;
  61. }
  62. else if (h < 240) {
  63. return n1 + (n2 - n1) * (240.0f - h) / 60.0f;
  64. }
  65. return n1;
  66. }
  67. /**
  68. * Converts from RGB color space to HLS colorspace.
  69. */
  70. private static float[] rgbToHLS(int rgb, float[] hls) {
  71. float r = ((rgb & 0xFF0000) >> 16) / 255.0f;
  72. float g = ((rgb & 0xFF00) >> 8) / 255.0f;
  73. float b = (rgb & 0xFF) / 255.0f;
  74. /* calculate lightness */
  75. float max = Math.max(Math.max(r, g), b);
  76. float min = Math.min(Math.min(r, g), b);
  77. float l = (max + min) / 2.0f;
  78. float s = 0;
  79. float h = 0;
  80. if (max != min) {
  81. float delta = max - min;
  82. s = (l <= .5f) ? (delta / (max + min)) : (delta / (2.0f - max -min));
  83. if (r == max) {
  84. h = (g - b) / delta;
  85. }
  86. else if (g == max) {
  87. h = 2.0f + (b - r) / delta;
  88. }
  89. else {
  90. h = 4.0f + (r - g) / delta;
  91. }
  92. h *= 60.0f;
  93. if (h < 0) {
  94. h += 360.0f;
  95. }
  96. }
  97. if (hls == null) {
  98. hls = new float[3];
  99. }
  100. hls[0] = h;
  101. hls[1] = l;
  102. hls[2] = s;
  103. return hls;
  104. }
  105. /**
  106. * Creates and returns a new color derived from the passed in color.
  107. * The transformation is done in the HLS color space using the specified
  108. * arguments to scale.
  109. *
  110. * @param color Color to alter
  111. * @param hFactory Amount to scale the hue
  112. * @param lFactor Amount to scale the lightness
  113. * @param sFactory Amount to sacle saturation
  114. * @return newly created color
  115. */
  116. static Color adjustColor(Color color, float hFactor, float lFactor,
  117. float sFactor) {
  118. float h;
  119. float l;
  120. float s;
  121. synchronized(HLS_COLOR_LOCK) {
  122. float[] hls = rgbToHLS(color.getRGB(), HLS_COLORS);
  123. h = hls[0];
  124. l = hls[1];
  125. s = hls[2];
  126. }
  127. h = Math.min(360, hFactor * h);
  128. l = Math.min(1, lFactor * l);
  129. s = Math.min(1, sFactor * s);
  130. return new ColorUIResource(hlsToRGB(h, l, s));
  131. }
  132. protected GTKColorType(String name) {
  133. super(name);
  134. }
  135. }