Last active
December 17, 2015 16:49
-
-
Save amano41/5642056 to your computer and use it in GitHub Desktop.
Processing で補色残像のデモプログラムを作ってみました。
http://amano41.hateblo.jp/entry/2013/05/24/173703
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
/* ====================================================================== * | |
* 補色残像のデモプログラム | |
* | |
* 参考) | |
* http://www.johnsadowski.com/color_illusion_tutorial.html | |
* http://www.adobe.com/jp/devnet/pdf/pdf_reference_archive.html | |
* ====================================================================== */ | |
static final String STIMULUS = "stimulus.jpg"; // 使用する画像 | |
static final int DURATION = 30; // 呈示時間(秒) | |
PImage grayImage; | |
PImage compImage; | |
int count; | |
void setup() { | |
// グレースケール画像 | |
grayImage = loadImage(STIMULUS); | |
grayImage.filter(GRAY); // グレースケール化 | |
// 補色画像 | |
compImage = loadImage(STIMULUS); | |
setLuminance(compImage, 128); // 輝度の調整 | |
compImage.filter(INVERT); // 階調の反転 | |
size(compImage.width, compImage.height); | |
frameRate(10); | |
count = 0; | |
} | |
void draw() { | |
if (count++ < DURATION * frameRate) { | |
image(compImage, 0, 0); | |
noStroke(); | |
fill(0); | |
ellipse(width/2, height/2, 10, 10); // 注視点 | |
} | |
else { | |
image(grayImage, 0, 0); | |
} | |
} | |
void mousePressed() { | |
if (count >= DURATION * frameRate) { | |
count = 0; | |
} | |
} | |
void keyPressed() { | |
if (key == 's' || key == 'S') { | |
grayImage.save("gray_image.jpg"); | |
compImage.save("comp_image.jpg"); | |
println("image saved."); | |
} | |
} | |
float luminance(float r, float g, float b) { | |
return 0.299 * r + 0.587 * g + 0.114 * b; | |
} | |
void setLuminance(PImage img, float lum) { | |
img.loadPixels(); | |
for (int i = 0; i < img.pixels.length; i++) { | |
color c = img.pixels[i]; | |
float r = red(c); | |
float g = green(c); | |
float b = blue(c); | |
float d = lum - luminance(r, g, b); | |
img.pixels[i] = clipColor(r+d, g+d, b+d); | |
} | |
} | |
color clipColor(float r, float g, float b) { | |
float L = luminance(r, g, b); | |
float n = min(r, g, b); | |
float x = max(r, g, b); | |
if (n < 0) { | |
r = L + (((r - L) * L) / (L - n)); | |
g = L + (((g - L) * L) / (L - n)); | |
b = L + (((b - L) * L) / (L - n)); | |
} | |
if (x > 255) { | |
r = L + (((r - L) * (255 - L)) / (x - L)); | |
g = L + (((g - L) * (255 - L)) / (x - L)); | |
b = L + (((b - L) * (255 - L)) / (x - L)); | |
} | |
return color(r, g, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment