// Normalize
value = Math.abs(value);
// Deal with min case
if (value <= epsilon) {
return new Point(1, 255);
}
// Deal with max case
if (value >= 255) {
return new Point(255, 1);
}
// Remember if we invert
boolean inverted = false;
if (value < 1.0) {
value = 1.0F/value;
inverted = true;
}
// First approximation
int y = 1;
int x = (int) Math.round(value);
float ratio = (float) x;
float delta = Math.abs(value - ratio);
while (delta > epsilon) { // not close enough
// Increment y and compute a new x
y++;
x = (int) Math.round(y*value);
ratio = (float)x/(float)y;
delta = Math.abs(value - ratio);
}
return inverted ? new Point(y, x) : new Point(x, y);
}