xxxxxxxxxx
134
let pic;
let captured = false;
let refX, refY;
let hRef = 0;
let sRef = 0;
let bRef = 0;
let refColor = 0;
let squareSize = 10;
let hBuffer = 11;
let sBuffer = 20;
let bBuffer = 20;
let r, g, b;
let ourFrameRate = 15;
let loops=0;
let skip=6;
function preload() {
pic = loadImage('mimiYin.jpg');
}
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(1);
background(0);
pic.loadPixels();
aspect = pic.height / pic.width;
//copy(pic,0,0,pic.width,pic.height,0,0,width,width*aspect);
image(pic, 0,0);
}
function draw() {
//background(220);
frameRate(ourFrameRate);
colorChange();
//pic.updatePixels();
// if(frameCount % ourFrameRate/4 == 0) {
squareSize += 2;
// }
// if (squareSize > pic.width || squareSize > pic.height) {
// noLoop();
// }
}
function mouseClicked() {
refX = mouseX;
refY = mouseY;
squareSize=10;
//console.log(mouseX, mouseY);
console.log(refX, refY);
captureColor();
}
function captureColor() {
refColor = pic.get(refX, refY);
hRef = hue(refColor);
sRef = saturation(refColor);
bRef = brightness(refColor);
captured = true;
console.log('capture color H,S,B:');
console.log(hRef, sRef, bRef);
}
function colorChange() {
//console.log('colorChange called');
// for (let x = refY - (squareSize / 2); x < refY + (squareSize / 2); x++) {
// for (let y = refX - (squareSize / 2); y < refX + (squareSize / 2); y++) {
for (let y = refY - (squareSize / 2); y < refY + (squareSize / 2); y+=skip) {
for (let x = refX - (squareSize / 2); x < refX + (squareSize / 2); x+=skip) {
let index = (y * pic.width + x) * 4;
let r = pic.pixels[index];
let g = pic.pixels[index + 1];
let b = pic.pixels[index + 2];
//pic.pixels[index + 3] = 255;
let c = color(r, g, b);
let h = hue(c);
let s = saturation(c);
b = brightness(c);
//console.log('pixel', x, y)
//console.log(h, s, b);
if (h > hRef - hBuffer && h < hRef + hBuffer) {
hueMatch = true;
//console.log('hueMatch');
} else hueMatch = false;
if (s > sRef - sBuffer && s < sRef + sBuffer) {
satMatch = true;
//console.log('satMatch');
} else satMatch = false;
if (b > bRef - bBuffer && b < bRef + bBuffer) {
briteMatch = true;
//console.log('briteMatch');
} else briteMatch = false;
if (dist(x, y, refX, refY) < squareSize / 2) {
withinCircle = true;
//console.log('withinCircle');
} else withinCircle = false;
if (x>0 && x<pic.width && y>0 && y<pic.height) {
withinPic=true;
} else withinPic=false;
if (satMatch && briteMatch && withinCircle && withinPic) {
// pic.pixels[index] = b;
// pic.pixels[index + 1] = r;
// pic.pixels[index + 2] = g;
let pointH = (h+180)&360;
let pointS = s;
let pointB = b;
colorMode(HSB, 360, 100, 100);
noStroke();
fill(pointH, pointS, pointB);
//strokeWeight(skip);
circle(x,y,skip);
}
}
}
}