src/Pure/GUI/color_value.scala
author wenzelm
Tue, 24 Sep 2013 20:41:28 +0200
changeset 53853 e8430d668f44
parent 53783 f5e9d182f645
child 56546 902960859c66
permissions -rw-r--r--
more quasi-generic PIDE modules (NB: Swing/JFX needs to be kept separate from non-GUI material);

/*  Title:      Pure/GUI/color_value.scala
    Module:     PIDE-GUI
    Author:     Makarius

Cached color values.
*/

package isabelle


import java.awt.Color


object Color_Value
{
  private var cache = Map.empty[String, Color]

  def parse(s: String): Color =
  {
    val i = java.lang.Long.parseLong(s, 16)
    val r = ((i >> 24) & 0xFF).toInt
    val g = ((i >> 16) & 0xFF).toInt
    val b = ((i >> 8) & 0xFF).toInt
    val a = (i & 0xFF).toInt
    new Color(r, g, b, a)
  }

  def print(c: Color): String =
  {
    val r = new java.lang.Integer(c.getRed)
    val g = new java.lang.Integer(c.getGreen)
    val b = new java.lang.Integer(c.getBlue)
    val a = new java.lang.Integer(c.getAlpha)
    String.format("%02x%02x%02x%02x", r, g, b, a).toUpperCase
  }

  def apply(s: String): Color =
    synchronized {
      cache.get(s) match {
        case Some(c) => c
        case None =>
          val c = parse(s)
          cache += (s -> c)
          c
      }
    }
}