Below is some p5.js code that might accomplish what you are trying to do. Note what the color_checker
function does. Pass it a color along with ranges for the red, green, blue, and alpha components, and it will return true
if all components fall within their designated ranges, and false
otherwise.
The code below draws a bluish square, then checks the colors of all the pixels. The bluish pixels are turned to red. It can easily be translated to Java.
// p5.js code
function setup() {
createCanvas(200, 200);
frameRate(1); // enable observer to see color change
}
function draw() {
background(255, 255, 0);
noStroke();
fill(0, 0, 216); // note that blue component is 216
rect(width / 4, height / 4, width / 2, height / 2);
if (frameCount == 2) {
for (let x = 0; x <= width; x++) {
for (y = 0; y <= height; y++) {
c = get(x, y);
// note range for blue
if (color_checker(c, 0, 255, 0, 255, 210, 220, 0, 255)) {
fill(255, 0, 0);
square(x, y, 1);
}
}
}
noLoop();
}
}
function color_checker(c, r_lo, r_hi, g_lo, g_hi, b_lo, b_hi, a_lo, a_hi) {
/*
given a color object, and low and high values (inclusive), for r, g, b, and a,
return:
true if all color components are within the specifed ranges,
otherwise false
*/
let r = red(c);
let g = green(c);
let b = blue(c);
let a = alpha(c);
return (
(r >= r_lo) && (r <= r_hi) &&
(g >= g_lo) && (g <= g_hi) &&
(b >= b_lo) && (b <= b_hi) &&
(a >= a_lo) && (a <= a_hi)
);
}