Created
February 2, 2012 01:34
-
-
Save tatsuro-ueda/1720811 to your computer and use it in GitHub Desktop.
color-watagashi-prototype.pde
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
import processing.video.*; | |
Capture cam; | |
void setup() { | |
size(320, 240); | |
cam = new Capture(this, 320, 240); | |
} | |
void draw() { | |
if (cam.available() == true) { | |
cam.read(); | |
image(cam, 0, 0); | |
// 色値をRGB別に積算する | |
float sum_red = 0; | |
float sum_green = 0; | |
float sum_blue = 0; | |
for (int x = 0; x < cam.width; x++) | |
{ | |
for (int y = 0; y < cam.height; y++) | |
{ | |
color c = cam.get(x, y); | |
float tmp_red = red(c); | |
float tmp_green = green(c); | |
float tmp_blue = blue(c); | |
sum_red += tmp_red; | |
sum_green += tmp_green; | |
sum_blue += tmp_blue; | |
} | |
} | |
// RGB別に色値の平均を求める | |
int R = (int)(sum_red / (cam.width * cam.height)); | |
int G = (int)(sum_green / (cam.width * cam.height)); | |
int B = (int)(sum_blue / (cam.width * cam.height)); | |
// 最小色値を求める | |
int RGBlist[] = {R, G, B}; | |
int RGBmin = RGBlist[0]; | |
for (int i = 0; i < 3; i++) | |
{ | |
if (RGBlist[i] < RGBmin) | |
{ | |
RGBmin = RGBlist[i]; | |
} | |
} | |
// 最小色値に基づいてRGB色値を正規化する | |
int heightR, heightG, heightB; | |
if (RGBmin != 0) | |
{ | |
heightR = cam.height * (R-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin)); | |
heightG = cam.height * (G-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin)); | |
heightB = cam.height * (B-RGBmin) / ((R-RGBmin)+(G-RGBmin)+(B-RGBmin)); | |
} | |
else | |
{ | |
heightR = 0; heightG = 0; heightB = 0; // 真っ暗なとき | |
} | |
// 矩形を描画する | |
colorMode(RGB, 256); | |
if(heightR != 0) | |
{ | |
fill(255, 0, 0, 64); | |
rect(0, 0, cam.width, heightR); | |
} | |
if(heightG != 0) | |
{ | |
fill(0, 255, 0, 64); | |
rect(0, heightR, cam.width, heightR + heightG); | |
} | |
if(heightB != 0) | |
{ | |
fill(0, 0, 255, 64); | |
rect(0, heightR + heightG, cam.width, heightR + heightG + heightB); | |
} | |
println( | |
"R: " + R + " " + | |
"G: " + G + " " + | |
"B: " + B + " " + | |
"RGBmin: " + RGBmin + " " + | |
"heightR: " + heightR + " " + | |
"heightG: " + heightG + " " + | |
"heightB: " + heightB + " " | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment