1 /*
   2  * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.openjdk.buildtools.generatenimbus;
  27 
  28 import javax.xml.stream.XMLStreamReader;
  29 import java.util.Optional;
  30 
  31 class Matte extends Paint {
  32     private int red;
  33     private int green;
  34     private int blue;
  35     private int alpha;
  36 
  37     private String uiDefaultParentName = null;
  38     private float hueOffset = 0;
  39     private float saturationOffset = 0;
  40     private float brightnessOffset = 0;
  41     private int alphaOffset = 0;
  42 
  43     private String componentPropertyName = null;
  44 
  45     public String getComponentPropertyName() { return componentPropertyName; }
  46 
  47     private boolean uiResource = true;
  48 
  49     Matte(XMLStreamReader reader) {
  50         red = Integer.parseInt(reader.getAttributeValue(null, "red"));
  51         green = Integer.parseInt(reader.getAttributeValue(null, "green"));
  52         blue = Integer.parseInt(reader.getAttributeValue(null, "blue"));
  53         alpha = Integer.parseInt(reader.getAttributeValue(null, "alpha"));
  54         uiDefaultParentName = reader.getAttributeValue(null,
  55                 "uiDefaultParentName");
  56         hueOffset = Float.parseFloat(reader.getAttributeValue(null,
  57                 "hueOffset"));
  58         saturationOffset = Float.parseFloat(reader.getAttributeValue(null,
  59                 "saturationOffset"));
  60         brightnessOffset = Float.parseFloat(reader.getAttributeValue(null,
  61                 "brightnessOffset"));
  62         alphaOffset = Integer.parseInt(reader.getAttributeValue(null,
  63                 "alphaOffset"));
  64         componentPropertyName = reader.getAttributeValue(null,
  65                 "componentPropertyName");
  66         uiResource = Boolean.parseBoolean(Optional.ofNullable(
  67                 reader.getAttributeValue(null, "uiResource")).orElse("true"));
  68     }
  69 
  70     public boolean isAbsolute() {
  71         return uiDefaultParentName == null;
  72     }
  73 
  74     public String getDeclaration() {
  75         if (isAbsolute()) {
  76             return String.format("new Color(%d, %d, %d, %d)",
  77                                  red, green, blue, alpha);
  78         } else {
  79             return String.format("decodeColor(\"%s\", %sf, %sf, %sf, %d)",
  80                     uiDefaultParentName, String.valueOf(hueOffset),
  81                     String.valueOf(saturationOffset),
  82                     String.valueOf(brightnessOffset), alphaOffset);
  83         }
  84     }
  85 
  86     public String write() {
  87         if (isAbsolute()) {
  88             return String.format("%s, %s, %s, %s", red, green, blue, alpha);
  89         } else {
  90             String s = String.format("\"%s\", %sf, %sf, %sf, %d",
  91                     uiDefaultParentName, String.valueOf(hueOffset),
  92                     String.valueOf(saturationOffset),
  93                     String.valueOf(brightnessOffset), alphaOffset);
  94             if (! uiResource) {
  95                 s += ", false";
  96             }
  97             return s;
  98         }
  99     }
 100 
 101     public ComponentColor createComponentColor(String variableName) {
 102         return new ComponentColor(componentPropertyName, variableName,
 103                 saturationOffset, brightnessOffset, alphaOffset);
 104     }
 105 }