Skip to content

Commit

Permalink
feat: tolerance based color equality (~1%)
Browse files Browse the repository at this point in the history
  • Loading branch information
frarees committed Oct 8, 2020
1 parent 3d48498 commit 0bb2818
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.pavelfatin.typometer.ExceptionHandler;
import com.pavelfatin.typometer.metrics.Metrics;
import com.pavelfatin.typometer.metrics.ColorUtil;
import com.pavelfatin.typometer.screen.ScreenAccessor;

import java.awt.*;
Expand Down Expand Up @@ -176,7 +177,8 @@ private static void typeSync(ExecutorService executor, Robot robot, ScreenAccess
int y = point.y;

for (int delay : delays) {
if (!accessor.getPixelColor((int) round(x), y).equals(metrics.getBackground())) {
if (!ColorUtil.equals(metrics.getBackground(), accessor.getPixelColor((int)round(x), y)))
{
throw new BenchmarkException("Previously undetected block cursor found.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.pavelfatin.typometer.benchmark;

import com.pavelfatin.typometer.screen.ScreenAccessor;
import com.pavelfatin.typometer.metrics.ColorUtil;

import java.awt.*;

Expand All @@ -29,8 +30,7 @@ static void waitForChange(ScreenAccessor accessor, int x, int y, Color previousC

do {
for (int i = 0; i < CHECK_PERIOD; i++) {
Color color = accessor.getPixelColor(x, y);
if (!previousColor.equals(color)) {
if (!ColorUtil.equals(accessor.getPixelColor(x, y), previousColor)) {
return;
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/pavelfatin/typometer/metrics/ColorUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017 Francisco Requena, https://frarees.github.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pavelfatin.typometer.metrics;

import java.awt.Color;

public class ColorUtil {
private ColorUtil() {
}

public static boolean equals(Color c1, Color c2, int tolerance) {
return (Math.abs(c1.getRed() - c2.getRed()) <= tolerance) &&
(Math.abs(c1.getGreen() - c2.getGreen()) <= tolerance) &&
(Math.abs(c1.getBlue() - c2.getBlue()) <= tolerance);
}

public static boolean equals(Color c1, Color c2) {
return equals(c1, c2, 3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,26 @@ private static Metrics metricsFrom(List<Rectangle> rectangles, BufferedImage ima

Point point = new Point((int) round(x1), (int) round(y1));

int background = image2.getRGB((int) round(x2 + step * 2), (int) round(y2));
Color background = new Color(image2.getRGB((int) round(x2 + step * 2), (int) round(y2)));

boolean blockCursor = image2.getRGB((int) round(x2 + step), (int) round(y2)) != background;
boolean blockCursor = !ColorUtil.equals(new Color(image2.getRGB((int) round(x2 + step), (int) round(y2))), background);

int availableLength = uniformLengthFrom(image2, new Point(point.x + (int) round(step * offset), point.y), background);

int length = (int) (floor(availableLength / step)) - (blockCursor ? 1 : 0);

return new Metrics(point, step, length + offset - 1, new Color(background), blockCursor);
return new Metrics(point, step, length + offset - 1, background, blockCursor);
}

private static int uniformLengthFrom(BufferedImage image, Point point, int color) {
private static int uniformLengthFrom(BufferedImage image, Point point, Color color) {
int width = image.getWidth();

int x;

for (x = point.x; x < width; x++) {
if (image.getRGB(x, point.y) != color) break;
if (!ColorUtil.equals(new Color(image.getRGB(x, point.y)), color)) {
break;
}
}

return x - point.x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ private static void assertPointsPresent(boolean expectation, BufferedImage image
for (int i = begin; i < end; i++) {
int ix = (int) round(x);

Color color = new Color(image.getRGB(ix, p.y));
boolean present = ColorUtil.equals(metrics.getBackground(), new Color(image.getRGB(ix, p.y)));

if (expectation) {
String message = String.format("Point %d must be present at (%d, %d)", i + 1, ix, p.y);
Assert.assertNotEquals(message, metrics.getBackground(), color);
Assert.assertFalse(message, present);
} else {
String message = String.format("Point %d must not be present at (%d, %d)", i + 1, ix, p.y);
Assert.assertEquals(message, metrics.getBackground(), color);
Assert.assertTrue(message, present);
}

x += metrics.getStep();
Expand Down

0 comments on commit 0bb2818

Please sign in to comment.