-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ef900d0
Showing
8 changed files
with
598 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
target | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>jto.colorscheme</groupId> | ||
<artifactId>ColorScheme</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>ColorScheme</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>processing</groupId> | ||
<artifactId>core</artifactId> | ||
<version>2.0.1</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<finalName>ColorScheme</finalName> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package jto.colorscheme; | ||
|
||
/** | ||
* This is the <code>Color</code> class. It represents an | ||
* RGB color. </br>It maintains three int values for red, green, and blue. | ||
* It also maintains an int representation of the RGB color identical | ||
* to the 'color' type in Processing. | ||
* | ||
* @author J. Taylor O'Connor (jtomusic@gmail.com) | ||
* @version 2013.08.15 | ||
* | ||
*/ | ||
public class Color { | ||
public int red; | ||
public int green; | ||
public int blue; | ||
|
||
private int rgb; | ||
|
||
/** | ||
* The default constructor for Color objects. | ||
*/ | ||
public Color() { | ||
} | ||
|
||
/** | ||
* Returns an int representation of the color identical to | ||
* the Processing 'color' type. | ||
* @return rgb the int color. | ||
*/ | ||
public int toInt() { | ||
return rgb; | ||
} | ||
|
||
/** | ||
* Sets the int rgb version of the color. | ||
* | ||
* @param rgb the color. | ||
*/ | ||
public void setRgb(int rgb) { | ||
this.rgb = rgb; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package jto.colorscheme; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.ByteOrder; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.w3c.dom.Document; | ||
import org.w3c.dom.NamedNodeMap; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
import processing.core.PApplet; | ||
|
||
/** | ||
* | ||
* This is the ColorScheme class which holds a list of {@link ColorSet} objects | ||
* that contain {@link jto.colorscheme.Color} objects. </br>Currently, ColorScheme supports loading | ||
* .xml files created from http://colorschemedesigner.com </br>as well as | ||
* .ase files (Adobe Swatch Exchange). | ||
* | ||
* The .ase parsing code is a slightmy modified version of the code | ||
* from the kulerviewer by Daniel Weber. | ||
* | ||
* @author J. Taylor O'Connor (jtomusic@gmail.com) | ||
* @version 2013.08.15 | ||
*/ | ||
public class ColorScheme { | ||
private Palette palette; | ||
|
||
/** | ||
* This is the constructor for <code>ColorScheme</code> objects. | ||
* @param filename - the file to be parsed. | ||
*/ | ||
public ColorScheme(final String filename, final PApplet parent) { | ||
String fileName = parent.dataPath(filename); | ||
if(null == fileName) { | ||
throw new RuntimeException("Please enter a file name."); | ||
} | ||
|
||
try { | ||
this.palette = Parser.readFile(fileName, parent); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException("The color file " + fileName + "could not be found."); | ||
} catch (IOException e) { | ||
throw new RuntimeException("An error occured while parsing " + fileName); | ||
} | ||
} | ||
|
||
/** | ||
* This returns an {@link java.util.ArrayList} of all the {@link ColorSet} objects | ||
* in the palette. | ||
* | ||
* @return ArrayList<ColorSet> colorSets - the color sets. | ||
*/ | ||
public ArrayList<ColorSet> getColorSets() { | ||
return palette.getColorSets(); | ||
} | ||
|
||
/** | ||
* This returns an {@link java.util.ArrayList} of {@link jto.colorscheme.Color} objects | ||
* that contains all the colors in the palette. | ||
* @return ArrayList<Color> colors - all the colors. | ||
*/ | ||
public ArrayList<Color> getColors() { | ||
ArrayList<Color> colors = new ArrayList<Color>(); | ||
|
||
for(ColorSet colorSet : palette.getColorSets()) { | ||
for(Color c : colorSet.getColors()) { | ||
colors.add(c); | ||
} | ||
} | ||
|
||
return colors; | ||
} | ||
|
||
/** | ||
* Generates and returns an {@link java.util.ArrayList} of {@link Integer} | ||
* colors. You can use these the same way you would use the | ||
* Processing 'color' type. | ||
* | ||
* @return ArrayList<Integer> intArray - the colors as an Integer array. | ||
*/ | ||
public ArrayList<Integer> toIntArray() { | ||
ArrayList<Integer> intArray = new ArrayList<Integer>(); | ||
|
||
for(ColorSet colorSet : palette.getColorSets()) { | ||
for(Color c : colorSet.getColors()) { | ||
intArray.add(c.toInt()); | ||
} | ||
} | ||
|
||
return intArray; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package jto.colorscheme; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* The <code>ColorSet</code> class represents a set or group of colors.</br> | ||
* It maintains a list of {@link jto.colorscheme.Color} objects. | ||
* @author J. Taylor O'Connor (jtomusic@gmail.com) | ||
* @version 2013.08.15 | ||
* | ||
*/ | ||
public class ColorSet { | ||
|
||
private ArrayList<Color> colors = new ArrayList<Color>(); | ||
|
||
/** | ||
* The default constructor for ColorSet objects. | ||
*/ | ||
public ColorSet() {} | ||
|
||
/** | ||
* Adds a color to the list. | ||
* | ||
* @param c a color | ||
* | ||
*/ | ||
public void addColor(Color c) { | ||
colors.add(c); | ||
} | ||
|
||
/** | ||
* Returns the list of colors. | ||
* @return ArrayList<Color> colors - the list of colors. | ||
*/ | ||
public ArrayList<Color> getColors() { | ||
return colors; | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/jto/colorscheme/ImageColorSchemeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package jto.colorscheme; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import processing.core.PApplet; | ||
import processing.core.PImage; | ||
|
||
/** | ||
* This creates palettes based on image files. | ||
*/ | ||
class ImageColorSchemeGenerator { | ||
|
||
private static final int THRESHOLD = 5; | ||
private static final int GRAY_THRESHOLD = 5; | ||
|
||
private static final int NUM_COLORS = 5; | ||
|
||
static Palette readFile(String fileName, PApplet parent) { | ||
PImage image = parent.loadImage(fileName); | ||
image.loadPixels(); | ||
|
||
Map<Integer, List<Integer>> colorMap = generateColorMap(image.pixels, parent); | ||
|
||
List<Integer> topColors = new ArrayList<Integer>(); | ||
findTopColors(colorMap, topColors, parent, NUM_COLORS); | ||
|
||
ColorSet colorSet = new ColorSet(); | ||
for (Integer color : topColors) { | ||
Color c = new Color(); | ||
c.red = (int) parent.red(color); | ||
c.green = (int) parent.green(color); | ||
c.blue = (int) parent.blue(color); | ||
c.setRgb(color); | ||
colorSet.addColor(c); | ||
} | ||
Palette palette = new Palette(); | ||
palette.addColorSet(colorSet); | ||
|
||
return palette; | ||
} | ||
|
||
private static void findTopColors(final Map<Integer, List<Integer>> colorMap, | ||
List<Integer> topColors, final PApplet parent, int numColors) { | ||
if (numColors == 0) { | ||
return; | ||
} | ||
int longest = 0; | ||
Integer longestKey = -1; | ||
for (Integer key : colorMap.keySet()) { | ||
List<Integer> colors = colorMap.get(key); | ||
if (colors.size() > longest) { | ||
longest = colors.size(); | ||
longestKey = key; | ||
} | ||
} | ||
topColors.add(longestKey); | ||
colorMap.remove(longestKey); | ||
findTopColors(colorMap, topColors, parent, numColors - 1); | ||
} | ||
|
||
private static Map<Integer, List<Integer>> generateColorMap(final int[] pixels, | ||
final PApplet parent) { | ||
Map<Integer, List<Integer>> colorMap = new HashMap<Integer, List<Integer>>(); | ||
for (int color : pixels) { | ||
if (isGray(color, parent)) { | ||
continue; | ||
} | ||
boolean categoryExists = false; | ||
for (Integer key : colorMap.keySet()) { | ||
if (isCloseToColor(key, color, parent)) { | ||
colorMap.get(key).add(color); | ||
categoryExists = true; | ||
break; | ||
} | ||
} | ||
if (!categoryExists) { | ||
List<Integer> colorList = new ArrayList<Integer>(); | ||
colorList.add(color); | ||
colorMap.put(color, colorList); | ||
} | ||
} | ||
return colorMap; | ||
} | ||
|
||
private static boolean isGray(final int color, final PApplet parent) { | ||
if (parent.abs(parent.red(color) - parent.blue(color)) < GRAY_THRESHOLD | ||
&& parent.abs(parent.blue(color) - parent.green(color)) < GRAY_THRESHOLD) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isCloseToColor(final int color, final int colorToTest, | ||
final PApplet parent) { | ||
if ((parent.abs(parent.red(color) - parent.red(colorToTest)) < THRESHOLD) && | ||
(parent.abs(parent.green(color) - parent.green(colorToTest)) < THRESHOLD) && | ||
(parent.abs(parent.blue(color) - parent.blue(colorToTest)) < THRESHOLD)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package jto.colorscheme; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* This class represents a palette of colors. | ||
* It maintains a list of {@link jto.colorscheme.ColorSet} objects. | ||
* | ||
* @author J. Taylor O'Connor (jtomusic@gmail.com) | ||
* @version 2013.08.15 | ||
* | ||
*/ | ||
public class Palette { | ||
|
||
private ArrayList<ColorSet> colorSets = new ArrayList<ColorSet>(); | ||
|
||
/** | ||
* default constructor for Palette objects. | ||
*/ | ||
public Palette() { | ||
|
||
} | ||
|
||
/** | ||
* Adds a ColorSet to the palette. | ||
* | ||
* @param colorSet the color set. | ||
*/ | ||
public void addColorSet(ColorSet colorSet) { | ||
this.colorSets.add(colorSet); | ||
} | ||
|
||
/** | ||
* Returns the list of colorSets in the palette. | ||
* | ||
* @return colorSets the color sets. | ||
*/ | ||
public ArrayList<ColorSet> getColorSets() { | ||
return colorSets; | ||
} | ||
} |
Oops, something went wrong.