xxxxxxxxxx
108
// P_1_2_2_01
// updated by Cassie Tarakajian
//
// Generative Gestaltung – Creative Coding im Web
// ISBN: 978-3-87439-902-9, First Edition, Hermann Schmidt, Mainz, 2018
// Benedikt Groß, Hartmut Bohnacker, Julia Laub, Claudius Lazzeroni
// with contributions by Joey Lee and Niels Poldervaart
// Copyright 2018
//
// http://www.generative-gestaltung.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* extract and sort the color palette of an image
*
* MOUSE
* position x : resolution
*
* KEYS
* 1-4 : load different images
* 5 : no color sorting
* 6 : sort colors on hue
* 7 : sort colors on saturation
* 8 : sort colors on brightness
* 9 : sort colors on greyscale (luminance)
* s : save png
* c : save color palette
*/
var img;
var colors = [];
var sortMode = null;
function preload() {
loadImage('data/one.jpg', setImage);
}
function setup() {
createCanvas(600, 600);
noCursor();
noStroke();
noLoop();
}
function draw() {
doSort();
}
function doSort() {
// var tileCount = floor(width / max(mouseX, 5));
var tileCount = 150;
var rectSize = width / tileCount;
img.loadPixels();
colors = [];
for (var gridY = 0; gridY < tileCount; gridY++) {
for (var gridX = 0; gridX < tileCount; gridX++) {
var px = int(gridX * rectSize);
var py = int(gridY * rectSize);
var i = (py * img.width + px) * 4;
var c = color(img.pixels[i], img.pixels[i + 1], img.pixels[i + 2], img.pixels[i + 3]);
colors.push(c);
}
}
gd.sortColors(colors, sortMode);
var i = 0;
for (var gridY = 0; gridY < tileCount; gridY++) {
for (var gridX = 0; gridX < tileCount; gridX++) {
fill(colors[i]);
rect(gridX * rectSize, gridY * rectSize, rectSize, rectSize);
i++;
}
}
}
function keyReleased() {
if (key == 'c' || key == 'C') writeFile([gd.ase.encode(colors)], gd.timestamp(), 'ase');
if (key == 's' || key == 'S') saveCanvas(gd.timestamp(), 'png');
if (key == '1') loadImage('data/one.jpg', setImage);
if (key == '2') loadImage('data/two.jpg', setImage);
if (key == '3') loadImage('data/three.jpg', setImage);
if (key == '4') loadImage('data/four.jpg', setImage);
if (key == '5') sortMode = null;
if (key == '6') sortMode = gd.HUE;
if (key == '7') sortMode = gd.SATURATION;
if (key == '8') sortMode = gd.BRIGHTNESS;
if (key == '9') sortMode = gd.GRAYSCALE;
if (key == 'd') doSort();
}
function setImage(loadedImageFile) {
img = loadedImageFile;
}