| author | wenzelm | 
| Sun, 22 Jan 2023 22:26:50 +0100 | |
| changeset 77044 | a4380a2d6d2c | 
| parent 75393 | 87ebf5a50283 | 
| permissions | -rw-r--r-- | 
| 
53783
 
f5e9d182f645
clarified location of GUI modules (which depend on Swing of JFX);
 
wenzelm 
parents: 
49322 
diff
changeset
 | 
1  | 
/* Title: Pure/GUI/color_value.scala  | 
| 49294 | 2  | 
Author: Makarius  | 
3  | 
||
4  | 
Cached color values.  | 
|
5  | 
*/  | 
|
6  | 
||
7  | 
package isabelle  | 
|
8  | 
||
9  | 
||
10  | 
import java.awt.Color  | 
|
| 62570 | 11  | 
import java.util.Locale  | 
| 49294 | 12  | 
|
13  | 
||
| 75393 | 14  | 
object Color_Value {
 | 
| 49294 | 15  | 
private var cache = Map.empty[String, Color]  | 
16  | 
||
| 75393 | 17  | 
  def parse(s: String): Color = {
 | 
| 49294 | 18  | 
val i = java.lang.Long.parseLong(s, 16)  | 
19  | 
val r = ((i >> 24) & 0xFF).toInt  | 
|
20  | 
val g = ((i >> 16) & 0xFF).toInt  | 
|
21  | 
val b = ((i >> 8) & 0xFF).toInt  | 
|
22  | 
val a = (i & 0xFF).toInt  | 
|
23  | 
new Color(r, g, b, a)  | 
|
24  | 
}  | 
|
25  | 
||
| 75393 | 26  | 
  def print(c: Color): String = {
 | 
| 71381 | 27  | 
val r = java.lang.Integer.valueOf(c.getRed)  | 
28  | 
val g = java.lang.Integer.valueOf(c.getGreen)  | 
|
29  | 
val b = java.lang.Integer.valueOf(c.getBlue)  | 
|
30  | 
val a = java.lang.Integer.valueOf(c.getAlpha)  | 
|
| 62570 | 31  | 
Word.uppercase(String.format(Locale.ROOT, "%02x%02x%02x%02x", r, g, b, a))  | 
| 49294 | 32  | 
}  | 
33  | 
||
34  | 
def apply(s: String): Color =  | 
|
35  | 
    synchronized {
 | 
|
36  | 
      cache.get(s) match {
 | 
|
37  | 
case Some(c) => c  | 
|
38  | 
case None =>  | 
|
39  | 
val c = parse(s)  | 
|
40  | 
cache += (s -> c)  | 
|
41  | 
c  | 
|
42  | 
}  | 
|
43  | 
}  | 
|
44  | 
}  |