Brightness
(Topics->ImageProcessing->Brightness)
マウスを動かすことで妖しい輝きを放ちます。
/** * Brightness
* by Daniel Shiffman.
*
* Adjusts the brightness of part of the image
* Pixels closer to the mouse will appear brighter.
*/
PImage img;
void setup() {
size(200, 200);
frameRate(30);
img = loadImage("wires.jpg");
img.loadPixels();
// Only need to load the pixels[] array once, because we're only
// manipulating pixels[] inside draw(), not drawing shapes.
loadPixels(); //この関数でpixels[]配列に画素を読み込むことができます
}
void draw() {
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++ ) {
// Calculate the 1D location from a 2D grid
int loc = x + y*img.width; //ここで画素のサイズから配列上のアドレスを算出してます
// Get the R,G,B values from image
float r,g,b;
r = red (img.pixels[loc]); //locで好きな位置の画素を参照。red()で赤成分のみ抽出
//g = green (img.pixels[loc]);
//b = blue (img.pixels[loc]);
// Calculate an amount to change brightness based on proximity to the mouse
float maxdist = 50;//dist(0,0,width,height); //ここで明かりの強さを変えられます
float d = dist(x,y,mouseX,mouseY); //マウスと現在の点のユークリッド距離を算出
float adjustbrightness = 255*(maxdist-d)/maxdist;
r += adjustbrightness;
//g += adjustbrightness;
//b += adjustbrightness;
// Constrain RGB to make sure they are within 0-255 color range
// RGB値を最終的に0-255に制限しなければなりません
r = constrain(r,0,255);
//g = constrain(g,0,255);
//b = constrain(b,0,255);
// Make a new color and set pixel in the window
//color c = color(r,g,b);
color c = color(r);
pixels[y*width + x] = c; //ピクセルの値を算出した値に置き換えます
}
}
updatePixels(); //この関数でpixels[]から画像への更新が行われます
}