xxxxxxxxxx
37
// data from https://developers.google.com/earth-engine/datasets/catalog/NOAA_DMSP-OLS_NIGHTTIME_LIGHTS
let nightsky;
const SAMPLE_WIDTH = 100;
const SAMPLE_HEIGHT = 100;
const rgb2luma = (r,g,b) => 0.2989 * r + 0.5870 * g + 0.1140 * b;
const rgb2lumahelper = (getres) => rgb2luma(getres[0], getres[1], getres[2]);
function preload() {
nightsky = loadImage('nightsky.png')
}
function setup() {
createCanvas(800, 600);
noLoop();
}
function draw() {
background(220);
image(nightsky, 0,0);
}
function mousePressed() {
let avg = 0;
for (let i = -SAMPLE_WIDTH / 2; i < SAMPLE_WIDTH / 2; i++) {
for (let j = -SAMPLE_HEIGHT / 2; j < SAMPLE_HEIGHT / 2; j++) {
avg += rgb2lumahelper(get(mouseX + i, mouseY + j)) / (SAMPLE_WIDTH * SAMPLE_HEIGHT);
}
}
noFill();
stroke('red');
rect(mouseX - SAMPLE_WIDTH / 2, mouseY - SAMPLE_HEIGHT / 2, SAMPLE_WIDTH, SAMPLE_HEIGHT);
console.log(avg);
}