Skip to content

Instantly share code, notes, and snippets.

@monkstone
Created March 28, 2017 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/14e2581486e87cf56c61d3f0c717af30 to your computer and use it in GitHub Desktop.
Save monkstone/14e2581486e87cf56c61d3f0c717af30 to your computer and use it in GitHub Desktop.
Color Harmony
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import processing.core.*;
import com.cage.colorharmony.*;
import java.util.ArrayList;
public class Palettes extends PApplet {
/**
* ***************************************************************************
*
* PALETTES - an example for the ColorHarmony Processing library
*
* This example will show the generated harmonized palettes for the four
* different harmonizing types: - MONOCHROMATIC - ANALOGOUS - COMPLEMENTARY
* - TRIADS
*
* Used methods: - ColorHarmony.Analogous() - ColorHarmony.Complementary() -
* ColorHarmony.Monochromatic() - ColorHarmony.P52Hex() -
* ColorHarmony.printVersion() - ColorHarmony.RandomHexColor() -
* ColorHarmony.Triads()
*
* Interaction: Click with your mouse on the sketch to select a new random
* base color
*
* Library page: http://cagewebdev.com/colorharmony-processing-library/
*
* Author: Rolf van Gelder http://rvg.cage.nl, http://cagewebdev.com,
* info@cagewebdev.com
*
* Release: 03/17/2017
*
* (c) 2017 Rolf van Gelder - CAGE Web Design
*
****************************************************************************
*/
// CREATE COLORHARMONY INSTANCE (DEFAULT CONSTRUCTOR)
ColorHarmony colorHarmony = new ColorHarmony(this);
// CREATE COLORHARMONY INSTANCE
// WITH min AND max VALUES FOR SATURATION AND LUMINOSITY
// DEFAULTS: minS = 10, maxS = 60, minL = 20, maxL = 60
// SATURATION: 0..100
// LUMOSITY: 0..100
//ColorHarmony colorHarmony = new ColorHarmony(this, 10, 60, 20, 60);
// HARMONIZING TYPES
final static int TYPE_MONOCHROMATIC = 0;
final static int TYPE_ANALOGOUS = 1;
final static int TYPE_COMPLEMENTARY = 2;
final static int TYPE_TRIADS = 3;
// LAYOUT FOR SHOWING THE PALETTES
int swatchSize = 80;
int swatchSpacing = 10;
int headerHeight = 45;
int titleHeight = 40;
int paletteSpacing = 30;
int paletteWidth = 4 * swatchSize + 3 * swatchSpacing;
int paletteHeight = titleHeight + 2 * swatchSize + swatchSpacing;
// COLOR VALUES FOR THE DIFFERENT HARMONIZING TYPES
String[] paletteMonochromatic = new String[8];
String[] paletteAnalogous = new String[8];
String[] paletteComplementary = new String[8];
String[] paletteTriads = new String[8];
// BASE COLOR FOR THE GENERATED PALETTES
String baseColorHex = "FF00FF";
/**
* ***************************************************************************
*
* SETUP
*
****************************************************************************
*/
@Override
public void setup() {
// CANVAS WIDTH AND HEIGHT
int w = 3 * paletteSpacing + 2 * paletteWidth;
int h = headerHeight + 2 * paletteHeight + paletteSpacing;
// FOR PROCESSING v2.x
//size(w, h);
// FOR PROCESSING v3.x
surface.setSize(w, h);
// WRITE THE VERSION OF THE LIBRARY TO THE CONSOLE
colorHarmony.printVersion();
if (baseColorHex.equals("")) {
// GENERATE A RANDOM BASE COLOR
baseColorHex = colorHarmony.RandomHexColor();
}
// CREATE THE FOUR PALETTES
palettes.add(new Palette(paletteSpacing, headerHeight, TYPE_MONOCHROMATIC, "MONOCHROMATIC"));
palettes.add(new Palette(2 * paletteSpacing + paletteWidth, headerHeight, TYPE_ANALOGOUS, "ANALOGOUS"));
palettes.add(new Palette(paletteSpacing, headerHeight + paletteHeight, TYPE_COMPLEMENTARY, "COMPLEMENTARY"));
palettes.add(new Palette(2 * paletteSpacing + paletteWidth, headerHeight + paletteHeight, TYPE_TRIADS, "TRIADS"));
// DEFAULT LAYOUT SETTINGS
textAlign(CENTER);
textSize(20);
fill(0);
} // setup()
/**
* ***************************************************************************
*
* DRAW
*
****************************************************************************
*/
@Override
public void draw() {
background(255);
// DISPLAY THE HEADER
text("ColorHarmony - Processing Library (base color: #" + baseColorHex + ")", width >> 1, 30);
// DISPLAY THE FOUR PALETTES
for (int i = 0; i < palettes.size(); i++) {
palettes.get(i).display();
}
} // draw()
/**
* ***************************************************************************
*
* UPDATE THE PALETTES BASED ON A NEW (RANDOM) BASE COLOR
*
****************************************************************************
*/
public void updatePalettes() {
// GET A RANDOM BASE COLOR
baseColorHex = colorHarmony.RandomHexColor();
// CHANGE THE PALETTES BASED ON THE NEW BASE COLOR
for (int i = 0; i < palettes.size(); i++) {
palettes.get(i).loadColors();
}
} // updatePalettes()
/**
* ***************************************************************************
*
* MOUSE HANDLER
*
****************************************************************************
*/
@Override
public void keyPressed() {
if (key == 'm' || key == 'M') {
System.out.println("# Monochromatic Palette base color #" + baseColorHex);
int[] colors = colorHarmony.Monochromatic(baseColorHex);
StringBuilder palette = fromColors(colors);
System.out.println(palette.toString());
}
if (key == 'c' || key == 'C') {
System.out.println("# Complimentary Palette base color #" + baseColorHex);
int[] colors = colorHarmony.Complementary(baseColorHex);
StringBuilder palette = fromColors(colors);
System.out.println(palette.toString());
}
if (key == 'a' || key == 'A') {
System.out.println("# Analgous Palette base color #" + baseColorHex);
int[] colors = colorHarmony.Analogous(baseColorHex);
StringBuilder palette = fromColors(colors);
System.out.println(palette.toString());
}
if (key == 't' || key == 'T') {
System.out.println("# Triad Palette base color #" + baseColorHex);
int[] colors = colorHarmony.Triads(baseColorHex);
StringBuilder palette = fromColors(colors);
System.out.println(palette.toString());
}
} // keyPressed()
public StringBuilder fromColors(int[] cols) {
StringBuilder palette = new StringBuilder("%w(");
for (int col : cols) {
palette.append('#');
palette.append(colorHarmony.P52Hex(col));
char end = col == cols[cols.length - 1] ? ')' : ' ';
palette.append(end);
}
return palette;
}
/**
* ***************************************************************************
*
* MOUSE HANDLER
*
****************************************************************************
*/
@Override
public void mousePressed() {
// UPDATE THE PALETTES BASED ON A NEW (RANDOM) BASE COLOR
updatePalettes();
} // mousePressed()
/**
* ***************************************************************************
*
* PALETTE CLASS FOR DISPLAYING THE PALETTES
*
****************************************************************************
*/
// ARRAY WITH THE FOUR PALETTES
ArrayList<Palette> palettes = new ArrayList<>();
/**
* ***************************************************************************
*
* PALETTE CLASS
*
****************************************************************************
*/
class Palette {
int xPos, yPos;
int paletteType;
String title;
int[] colors = new int[8];
int[] rgb = new int[3];
/**
* *************************************************************************
*
* CONSTRUCTOR
*
**************************************************************************
*/
Palette(int _xPos, int _yPos, int _paletteType, String _title) {
xPos = _xPos;
yPos = _yPos;
paletteType = _paletteType;
title = _title;
loadColors();
} // Palette()
/**
* *************************************************************************
*
* LOAD THE COLORS FOR THE FOUR PALETTES
*
**************************************************************************
*/
public final void loadColors() {
switch (paletteType) {
case TYPE_MONOCHROMATIC:
colors = colorHarmony.Monochromatic(baseColorHex);
break;
case TYPE_ANALOGOUS:
colors = colorHarmony.Analogous(baseColorHex);
break;
case TYPE_COMPLEMENTARY:
colors = colorHarmony.Complementary(baseColorHex);
break;
case TYPE_TRIADS:
colors = colorHarmony.Triads(baseColorHex);
break;
}
} // loadColors()
/**
* *************************************************************************
*
* DISPLAY THIS PALETTE
*
**************************************************************************
*/
public void display() {
// DISPLAY THE TITLE OF THE PALETTE
showTitle();
// DISPLAY THE 8 SWATCHES
showSwatches();
} // display()
/**
* *************************************************************************
*
* DISPLAY THE TITLE OF THE PALETTE
*
**************************************************************************
*/
public void showTitle() {
pushStyle();
textAlign(LEFT);
textSize(16);
fill(0);
text(title, xPos, yPos + 35);
popStyle();
} // showTitle()
/**
* *************************************************************************
*
* DISPLAY THE 8 SWATCHES
*
**************************************************************************
*/
public void showSwatches() {
pushStyle();
int x2 = xPos;
int y2 = yPos + titleHeight;
for (int i = 0; i < 8; i++) {
if (i == 4) {
y2 += swatchSize + swatchSpacing;
x2 = xPos;
}
fill(colors[i]);
stroke(200);
rect(x2, y2, swatchSize, swatchSize);
if (brightness(colors[i]) > 160) {
fill(0);
} else {
fill(255);
}
textAlign(LEFT);
textSize(13);
String hexCode = colorHarmony.P52Hex(colors[i]);
float tw = textWidth("#" + hexCode);
text("#" + hexCode, x2 + (swatchSize - tw) / 2, y2 + titleHeight + 7);
x2 += swatchSize + swatchSpacing;
} // for (int i = 0; i < 8; i++)
popStyle();
} // showSwatches()
} // Palette
static public void main(String[] passedArgs) {
String[] appletArgs = new String[]{"Palettes"};
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment