001package com.hfg.graphics; 002 003 004import com.hfg.html.attribute.HTMLColor; 005import com.hfg.util.CompareUtil; 006import com.hfg.util.collection.CollectionUtil; 007import com.hfg.xml.XMLTag; 008 009import java.awt.Color; 010import java.util.ArrayList; 011import java.util.Collection; 012import java.util.Collections; 013import java.util.List; 014 015 016//------------------------------------------------------------------------------ 017/** 018 * A set of 26 high contrast colors. 019 * From Green-armytage, Paul. 2010. "A Colour Alphabet and the Limits of Colour Coding." Colour: Design & Creativity 5 (10): 1-23. 020 * 021 * @author J. Alex Taylor, hairyfatguy.com 022 */ 023//------------------------------------------------------------------------------ 024// com.hfg XML/HTML Coding Library 025// 026// This library is free software; you can redistribute it and/or 027// modify it under the terms of the GNU Lesser General Public 028// License as published by the Free Software Foundation; either 029// version 2.1 of the License, or (at your option) any later version. 030// 031// This library is distributed in the hope that it will be useful, 032// but WITHOUT ANY WARRANTY; without even the implied warranty of 033// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 034// Lesser General Public License for more details. 035// 036// You should have received a copy of the GNU Lesser General Public 037// License along with this library; if not, write to the Free Software 038// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 039// 040// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 041// jataylor@hairyfatguy.com 042//------------------------------------------------------------------------------ 043 044public class ColorPaletteImpl implements ColorPalette, Comparable 045{ 046 private List<HTMLColor> mColors; 047 048 private int mCurrentIndex = 0; 049 050 051 052 //########################################################################## 053 // CONSTRUCTORS 054 //########################################################################## 055 056 //--------------------------------------------------------------------------- 057 public ColorPaletteImpl(Collection<? extends HTMLColor> inColors) 058 { 059 mColors = Collections.unmodifiableList(new ArrayList<>(inColors)); 060 } 061 062 //--------------------------------------------------------------------------- 063 public ColorPaletteImpl(XMLTag inXMLTag) 064 { 065 inXMLTag.verifyTagName(XML_COLOR_PALETTE); 066 067 List<XMLTag> subTags = inXMLTag.getSubtagsByName(XML_COLOR); 068 if (CollectionUtil.hasValues(subTags)) 069 { 070 mColors = new ArrayList<>(subTags.size()); 071 072 for (XMLTag subTag : subTags) 073 { 074 mColors.add(HTMLColor.valueOf(subTag.getUnescapedContent())); 075 } 076 } 077 } 078 079 //########################################################################## 080 // PUBLIC METHODS 081 //########################################################################## 082 083 //--------------------------------------------------------------------------- 084 public List<HTMLColor> getColorValues() 085 { 086 return mColors; 087 } 088 089 //--------------------------------------------------------------------------- 090 /** 091 Returns the next color from the palette or loops back to the beginning color if all colors have been used. 092 * @return the next color from the palette 093 */ 094 public HTMLColor nextColor() 095 { 096 HTMLColor color = null; 097 List<HTMLColor> values = getColorValues(); 098 if (CollectionUtil.hasValues(values)) 099 { 100 color = values.get(mCurrentIndex++); 101 102 if (mCurrentIndex >= values.size()) 103 { 104 mCurrentIndex = 0; 105 } 106 } 107 108 return color; 109 } 110 111 112 public XMLTag toXMLTag() 113 { 114 XMLTag tag = new XMLTag(XML_COLOR); 115 116 for(Color color : mColors) 117 { 118 tag.addSubtag(new XMLTag(XML_COLOR).setContent(new HTMLColor(color).toString())); 119 } 120 121 return tag; 122 } 123 124 //--------------------------------------------------------------------------- 125 @Override 126 public int compareTo(Object inObj2) 127 { 128 int result = -1; 129 130 if (inObj2 != null 131 && inObj2 instanceof ColorPaletteImpl) 132 { 133 ColorPaletteImpl colorPaletteImpl2 = (ColorPaletteImpl) inObj2; 134 135 result = CompareUtil.compare(mColors, colorPaletteImpl2.mColors); 136 } 137 138 return result; 139 } 140}