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