/** * Si existe la version de paleta 1.0, la actualizara a la 1.1 y renombrará la antigua version. * * @param palettesBasePath * @param colorTable */ public static void save_to_1_1(String palettesBasePath, ColorTable colorTable) { try { // Generar nuevo fichero KXmlSerializer parserOutput = new KXmlSerializer(); FileOutputStream out = new FileOutputStream(palettesBasePath + File.separator + colorTable.getName() + ".xml"); OutputStreamWriter writer = new OutputStreamWriter(out, "UTF8"); parserOutput.setOutput(writer); parserOutput.startDocument("UTF-8", null); parserOutput.startTag(null, "ColorTable"); parserOutput.attribute(null, "name", colorTable.getName()); parserOutput.attribute(null, "version", "1.1"); parserOutput.text("\n"); ArrayList items = colorTable.getColorItems(); for (int i = 0; i < items.size(); i++) { ColorItem colorItem = (ColorItem) items.get(i); parserOutput.startTag(null, "Color"); parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); if (colorItem.getNameClass() != null) parserOutput.attribute(null, "name", String.valueOf(colorItem.getNameClass())); else parserOutput.attribute(null, "name", ""); Color color = colorItem.getColor(); parserOutput.attribute( null, "rgb", String.valueOf(color.getRed() + "," + color.getGreen() + "," + color.getBlue())); parserOutput.attribute(null, "interpolated", String.valueOf(colorItem.getInterpolated())); parserOutput.endTag(null, "Color"); parserOutput.text("\n"); } for (int i = 0; i < items.size(); i++) { ColorItem colorItem = (ColorItem) items.get(i); parserOutput.startTag(null, "Alpha"); parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); parserOutput.attribute(null, "alpha", String.valueOf(colorItem.getColor().getAlpha())); parserOutput.attribute(null, "interpolated", String.valueOf(colorItem.getInterpolated())); parserOutput.endTag(null, "Alpha"); parserOutput.text("\n"); } parserOutput.endTag(null, "ColorTable"); parserOutput.text("\n"); parserOutput.endDocument(); // Cerrar nuevo fichero writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
/** * Lee una paleta del fichero xml de paletas y la carga en la tabla del panel. * * @param palettesPath Camino al fichero de paletas predefinidas. * @param paletteName Nombre de paleta a cargar desde el fichero xml. * @return Nombre de la paleta */ public static String loadPalette( String palettesBasePath, String paletteFileName, ArrayList items) { updateVersion(palettesBasePath); items.clear(); File palettesFile = new File(palettesBasePath + File.separator + paletteFileName); if (!palettesFile.exists()) return null; try { String paletteName = ""; ArrayList rows = new ArrayList(); KXmlParser parser = new KXmlParser(); FileInputStream in = new FileInputStream(palettesBasePath + File.separator + paletteFileName); InputStreamReader reader = new InputStreamReader(in, "UTF8"); parser.setInput(reader); int tag = parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "ColorTable"); for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeName(i).equals("name")) { paletteName = parser.getAttributeValue(i); } } tag = parser.nextTag(); while (!((tag == KXmlParser.END_TAG) && (parser.getName().equals("ColorTable")))) { try { if (tag == KXmlParser.START_TAG) { if (parser.getName().equals("Color")) { ColorItem colorItem = new ColorItem(); int a = 255; for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeName(i).equals("value")) { colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i))); ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i))); if (aux != null) a = aux.getColor().getAlpha(); } if (parser.getAttributeName(i).equals("name")) { colorItem.setNameClass((String) parser.getAttributeValue(i)); } if (parser.getAttributeName(i).equals("rgb")) { String rgb = parser.getAttributeValue(i); int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue(); int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))) .intValue(); int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())) .intValue(); colorItem.setColor(new Color(r, g, b, a)); } if (parser.getAttributeName(i).equals("interpolated")) { colorItem.setInterpolated( Double.parseDouble((String) parser.getAttributeValue(i))); } } rows.add(colorItem); continue; } if (parser.getName().equals("Alpha")) { ColorItem colorItem = new ColorItem(); for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeName(i).equals("value")) { colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i))); ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i))); if (aux != null) { colorItem.setNameClass(aux.getNameClass()); colorItem.setInterpolated(aux.getInterpolated()); colorItem.setColor( new Color( aux.getColor().getRed(), aux.getColor().getGreen(), aux.getColor().getBlue(), colorItem.getColor().getAlpha())); } } if (parser.getAttributeName(i).equals("alpha")) { int a = Integer.parseInt(parser.getAttributeValue(i)); colorItem.setColor( new Color( colorItem.getColor().getRed(), colorItem.getColor().getGreen(), colorItem.getColor().getBlue(), a)); } if (parser.getAttributeName(i).equals("interpolated")) { colorItem.setInterpolated( Double.parseDouble((String) parser.getAttributeValue(i))); } } rows.add(colorItem); continue; } } } finally { tag = parser.nextTag(); } } for (int i = 0; i < rows.size(); i++) items.add(rows.get(i)); reader.close(); return paletteName; } catch (FileNotFoundException fnfEx) { fnfEx.printStackTrace(); } catch (XmlPullParserException xmlEx) { System.out.println( "El fichero de paletas predeterminadas no tiene la estructura correcta:\n " + xmlEx.getMessage()); } catch (IOException e) { } return null; }
/** * Crea los ficheros que forman la paleta de color de la version 1.1 a traves de un XML que se le * pasa por parametro * * @param palettesPath */ private static void createVersionFromXML(String palettesBasePath, String xml) { new File(palettesBasePath).mkdir(); KXmlParser parser = new KXmlParser(); StringReader reader = new StringReader(xml); try { parser.setInput(reader); int tag = parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "palettes"); tag = parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "palette_list"); parser.skipSubTree(); parser.require(KXmlParser.END_TAG, null, "palette_list"); tag = parser.nextTag(); while (tag == KXmlParser.START_TAG) { parser.require(KXmlParser.START_TAG, null, "palette"); if (parser.getAttributeCount() == 2) { // Generar nuevo fichero KXmlSerializer parserOutput = new KXmlSerializer(); FileOutputStream fileOutputStream = new FileOutputStream( palettesBasePath + File.separator + parser.getAttributeValue(0) + ".xml"); parserOutput.setOutput(fileOutputStream, null); parserOutput.startDocument("UTF-8", null); parserOutput.startTag(null, "ColorTable"); parserOutput.attribute(null, "name", parser.getAttributeValue(0)); parserOutput.attribute(null, "version", "1.1"); tag = parser.nextTag(); parser.require(KXmlParser.START_TAG, null, "table"); tag = parser.nextTag(); parserOutput.text("\n"); ArrayList items = new ArrayList(); while (tag == KXmlParser.START_TAG) { parser.require(KXmlParser.START_TAG, null, "entry"); if (parser.getAttributeCount() == 3) { String rgb = parser .getAttributeValue(1) .substring( parser.getAttributeValue(1).indexOf(",") + 1, parser.getAttributeValue(1).length()); int a = Integer.valueOf( parser .getAttributeValue(1) .substring(0, parser.getAttributeValue(1).indexOf(","))) .intValue(); int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue(); int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))) .intValue(); int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue(); ColorItem colorItem = new ColorItem(); colorItem.setColor(new Color(r, g, b, a)); colorItem.setInterpolated(50); colorItem.setNameClass(parser.getAttributeValue(0)); colorItem.setValue(Double.parseDouble(parser.getAttributeValue(2))); items.add(colorItem); } tag = parser.nextTag(); parser.require(KXmlParser.END_TAG, null, "entry"); tag = parser.nextTag(); } parser.require(KXmlParser.END_TAG, null, "table"); tag = parser.nextTag(); ColorTable colorTable = new ColorTable(); colorTable.createPaletteFromColorItems(items, true); items = colorTable.getColorItems(); for (int i = 0; i < items.size(); i++) { ColorItem colorItem = (ColorItem) items.get(i); parserOutput.startTag(null, "Color"); parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); parserOutput.attribute(null, "name", String.valueOf(colorItem.getNameClass())); Color color = colorItem.getColor(); parserOutput.attribute( null, "rgb", String.valueOf(color.getRed() + "," + color.getGreen() + "," + color.getBlue())); parserOutput.attribute( null, "interpolated", String.valueOf(colorItem.getInterpolated())); parserOutput.endTag(null, "Color"); parserOutput.text("\n"); } for (int i = 0; i < items.size(); i++) { ColorItem colorItem = (ColorItem) items.get(i); parserOutput.startTag(null, "Alpha"); parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); parserOutput.attribute(null, "alpha", String.valueOf(colorItem.getColor().getAlpha())); parserOutput.attribute( null, "interpolated", String.valueOf(colorItem.getInterpolated())); parserOutput.endTag(null, "Alpha"); parserOutput.text("\n"); } parserOutput.endTag(null, "ColorTable"); parserOutput.text("\n"); parserOutput.endDocument(); // Cerrar nuevo fichero fileOutputStream.close(); } parser.require(KXmlParser.END_TAG, null, "palette"); tag = parser.nextTag(); } parser.require(KXmlParser.END_TAG, null, "palettes"); } catch (XmlPullParserException xmlEx) { System.out.println( "El fichero de paletas predeterminadas no tiene la estructura correcta:\n " + xmlEx.getMessage()); } catch (IOException e) { } }