001/* An attribute that specifies a color. 002 003 Copyright (c) 2003-2014 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.actor.gui; 029 030import java.awt.Color; 031 032import ptolemy.data.ArrayToken; 033import ptolemy.data.DoubleToken; 034import ptolemy.data.expr.Parameter; 035import ptolemy.data.type.ArrayType; 036import ptolemy.data.type.BaseType; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NameDuplicationException; 039import ptolemy.kernel.util.NamedObj; 040 041/////////////////////////////////////////////////////////////////// 042//// ColorAttribute 043 044/** 045 This is an attribute that specifies a color. The value of this 046 attribute is an array of four doubles in the form 047 {red, green, blue, alpha}, where each number is the range of 0.0 048 to 1.0. The 'alpha' term represents opacity, where 1.0 is opaque, 049 and 0.0 is fully transparent (invisible). 050 051 @author Edward A. Lee 052 @version $Id$ 053 @since Ptolemy II 4.0 054 @Pt.ProposedRating Yellow (eal) 055 @Pt.AcceptedRating Red (cxh) 056 */ 057public class ColorAttribute extends Parameter { 058 059 /** Construct an attribute with the given name. 060 * If the name argument is null, then the name is set to the empty 061 * string. Increment the version of the workspace. 062 * @param name The name of this attribute. 063 * @exception IllegalActionException If the name contains a period. 064 * @exception NameDuplicationException If the name coincides with 065 * an attribute already in the container. 066 */ 067 public ColorAttribute(String name) 068 throws IllegalActionException, NameDuplicationException { 069 super(); 070 setTypeEquals(new ArrayType(BaseType.DOUBLE)); 071 } 072 073 /** Construct an attribute with the given name contained by the 074 * specified container. The container argument must not be null, or a 075 * NullPointerException will be thrown. This attribute will use the 076 * workspace of the container for synchronization and version counts. 077 * If the name argument is null, then the name is set to the empty 078 * string. Increment the version of the workspace. 079 * @param container The container. 080 * @param name The name of this attribute. 081 * @exception IllegalActionException If the attribute is not of an 082 * acceptable class for the container, or if the name contains a period. 083 * @exception NameDuplicationException If the name coincides with 084 * an attribute already in the container. 085 */ 086 public ColorAttribute(NamedObj container, String name) 087 throws IllegalActionException, NameDuplicationException { 088 super(container, name); 089 setTypeEquals(new ArrayType(BaseType.DOUBLE)); 090 } 091 092 /////////////////////////////////////////////////////////////////// 093 //// public methods //// 094 095 /** Return the color as a Color object. 096 * @return The color as a Color object. 097 */ 098 public Color asColor() { 099 // NOTE: This also has to be handled by Query.java. 100 try { 101 ArrayToken spec = (ArrayToken) getToken(); 102 int length = 0; 103 104 if (spec != null) { 105 length = spec.length(); 106 } 107 108 // Default values allow us to tolerate incomplement specs. 109 float red = 0f; 110 float green = 0f; 111 float blue = 0f; 112 float alpha = 1.0f; 113 114 if (length > 0) { 115 red = (float) ((DoubleToken) spec.getElement(0)).doubleValue(); 116 } 117 118 if (length > 1) { 119 green = (float) ((DoubleToken) spec.getElement(1)) 120 .doubleValue(); 121 } 122 123 if (length > 2) { 124 blue = (float) ((DoubleToken) spec.getElement(2)).doubleValue(); 125 } 126 127 if (length > 3) { 128 alpha = (float) ((DoubleToken) spec.getElement(3)) 129 .doubleValue(); 130 } 131 132 return new Color(red, green, blue, alpha); 133 } catch (IllegalActionException ex) { 134 // getToken() failed for some reason. 135 return Color.black; 136 } 137 } 138}