1 19 20 25 26 package org.netbeans.modules.css.visual.model; 27 28 import java.awt.Color ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.StringTokenizer ; 33 import javax.swing.DefaultComboBoxModel ; 34 35 40 public class ColorModel { 41 private Color color = Color.BLACK; 42 43 private Map colorNameHexMap = CssProperties.getColorNameHexMap(); 44 45 public DefaultComboBoxModel getColorList(){ 46 return new ColorList(); 47 } 48 49 public Color getColor(){ 50 return color; 51 } 52 53 public void setColor(Color newColor){ 54 color = newColor; 55 } 56 57 public void setColor(String newColor){ 58 setColor(Color.BLACK); 59 if(newColor.startsWith(CssStyleData.NOT_SET)){ 60 return; 61 }else if(newColor.startsWith("#")){ setHexColor(newColor); 64 }else if (newColor.startsWith("rgb")){ try{ 66 String rgbString = newColor.substring(newColor.indexOf("(") + 1, newColor.indexOf(")")); StringTokenizer st = new StringTokenizer (rgbString,","); int red = Integer.parseInt(st.nextToken().trim()); 70 int green = Integer.parseInt(st.nextToken().trim()); 71 int blue = Integer.parseInt(st.nextToken().trim()); 72 color = new Color (red,green,blue); 73 }catch(Exception exc){ 74 color = Color.BLACK; 75 } 76 }else { 77 String hexValue = (String ) colorNameHexMap.get(newColor); 78 if(hexValue != null){ 79 setHexColor(hexValue); 80 } 81 } 82 } 83 84 public void setHexColor(String hexColor){ 85 int start = 0; 86 if(hexColor.startsWith("#")){ 87 start = 1; 88 } 89 try{ 90 int red = Integer.parseInt(hexColor.substring(start,start+2),16); 91 int green = Integer.parseInt(hexColor.substring(start+2, start+4),16); 92 int blue = Integer.parseInt(hexColor.substring(start+4, start+6),16); 93 color = new Color (red,green,blue); 94 }catch(Exception exc){ 95 color = Color.BLACK; 96 } 97 } 98 99 public String getHexColor(Color col){ 100 color = col; 101 return getHexColor(); 102 } 103 104 public String getHexColor(){ 105 String redHex = Integer.toHexString(color.getRed()); 106 if(redHex.length()<2) redHex = "0" + redHex; String greenHex = Integer.toHexString(color.getGreen()); 108 if(greenHex.length()<2) greenHex = "0" + greenHex; String blueHex = Integer.toHexString(color.getBlue()); 110 if(blueHex.length()<2) blueHex = "0" + blueHex; 112 String hexString = "#" + redHex + greenHex + blueHex; 114 return hexString; 115 } 116 117 public class ColorList extends DefaultComboBoxModel { 118 public ColorList(){ 119 addElement(CssStyleData.NOT_SET); 120 for(Iterator iter=colorNameHexMap.keySet().iterator(); iter.hasNext();){ 121 addElement(iter.next()); 122 } 123 } 124 } 125 126 } 127 | Popular Tags |