Skip to content

Instantly share code, notes, and snippets.

@S4K4K0
Created February 21, 2020 16:36
Show Gist options
  • Save S4K4K0/0348f4bb8c7fa3566cb0134e00183226 to your computer and use it in GitHub Desktop.
Save S4K4K0/0348f4bb8c7fa3566cb0134e00183226 to your computer and use it in GitHub Desktop.
int graphHeight = 300;
int graphWidth = 300;
int displayMargin = 100;
PImage img;
float x0 = 0, y0 = 0, x1 = 0, y1 = 0;
boolean dragging = false;
boolean defined = false;
color[] colorArray = new color[0];
void setup() {
size(400, 400);
surface.setResizable(true);
colorMode(RGB, 255);
noLoop();
selectInput("select GIF, JPEG, or PNG file:", "fileSelected");
while (img == null) delay (200);
float windowHeight = img.height + graphHeight + displayMargin;
if (windowHeight / img.width > displayHeight / displayWidth) { // 画面より縦長の場合
img.resize(0, displayHeight - (graphHeight + displayMargin));
} else { // 画面より横長の場合
img.resize(displayWidth - displayMargin, 0);
}
surface.setSize(img.width, img.height + graphHeight);
img.loadPixels();
stroke(0, 200, 50);
strokeWeight(2);
strokeCap(ROUND);
}
void draw() {
background(50);
image(img, 0, 0);
if (dragging) {
stroke(0, 200, 50);
line(x0, y0, mouseX, mouseY);
}
if (!dragging && x0 != x1 && y0 != y1) {
stroke(0, 200, 50);
line(x0, y0, x1, y1);
}
for (int i = 0; i < colorArray.length; i++) {
noStroke();
pushMatrix();
translate(width/2 - graphWidth/2, height - graphHeight);
fill(colorArray[i]);
rect(i, graphHeight-30, 1, 30);
// float bplot = map(brightness(colorArray[i]), 0, 255, graphHeight, 0); // brightness graph
// fill(255, 255, 255);
// rect(i, bplot, 2, 2);
float redPlot = map(red(colorArray[i]), 0, 255, graphHeight, 0);
fill(255, 0, 0);
rect(i, redPlot, 2, 2);
float greenPlot = map(green(colorArray[i]), 0, 255, graphHeight, 0);
fill(0, 255, 0);
rect(i, greenPlot, 2, 2);
float bluePlot = map(blue(colorArray[i]), 0, 255, graphHeight, 0);
fill(0, 0, 255);
rect(i, bluePlot, 2, 2);
float grayPlot = 0.2989*redPlot + 0.5866*greenPlot + 0.1144*bluePlot;
fill(255, 255, 255, 127);
rect(i, grayPlot, 2, 2);
popMatrix();
}
}
void getColorArray(float x0, float y0, float x1, float y1) {
float h0 = 0, h1 = 0, v0 = 0, v1 = 0;
float slope;
boolean steep = false;
colorArray = new color[0];
if (y1 == y0) { //線が水平な時
slope = 0;
h0 = x0;
h1 = x1;
v0 = y0;
v1 = y1;
} else if (x1 == x0) { //線が垂直なとき
slope = 0;
steep = true;
h0 = y0;
h1 = y1;
v0 = x0;
v1 = x1;
} else {
slope = (y1 - y0) / (x1 - x0);
steep = abs(slope) > 1;
if (steep) {
h0 = y0;
h1 = y1;
v0 = x0;
v1 = x1;
slope = (v1 - v0) / (h1 - h0);
} else {
h0 = x0;
h1 = x1;
v0 = y0;
v1 = y1;
slope = (v1 - v0) / (h1 - h0);
}
}
println("grad: " + slope);
float deltaH = (h1 - h0) / float(graphWidth);
//fill(204, 102, 0);
for (int i = 0; i < graphWidth; i++) {
int h = round(h0 + deltaH * i);
int nextH = round(h0 + deltaH * (i + 1));
int v;
int count = 0;
long sumR = 0, sumG = 0, sumB = 0;
color c;
int leftBoundary, rightBoundary;
if (h <= nextH) {
leftBoundary = h;
rightBoundary = nextH;
} else {
leftBoundary = nextH;
rightBoundary = h;
}
for (int j = leftBoundary; j <= rightBoundary; j++ ) {
v = round(v0 + (j - h0) * slope);
if (!steep) {
c = img.pixels[v * width + h];
//print("("+h+","+v+") ");
} else {
c = img.pixels[h * width + v];
//print("("+v+","+h+") ");
}
sumR += red(c);
sumG += green(c);
sumB += blue(c);
count++;
}
colorArray = append(colorArray, color(int(sumR/count), int(sumG/count), int(sumB/count)));
}
println("\n------");
}
/////////////////////////////////////////
// FILE SELECT
/////////////////////////////////////////
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
img = loadImage(selection.getAbsolutePath());
loop();
}
}
/////////////////////////////////////////
// MOUSE EVENT
/////////////////////////////////////////
void mousePressed() {
x0 = mouseX;
y0 = mouseY;
dragging = true;
}
void mouseReleased() {
dragging = false;
x1 = mouseX;
y1 = mouseY;
getColorArray(x0, y0, x1, y1);
//print("x0: "+x0);
//println(" x1: "+x1);
//print("y0: "+y0);
//println(" y1: "+y1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment