Skip to content

Commit 9c6cc49

Browse files
pftgqwencoder
andcommitted
docs: clarify color comparison options — tolerance, perceptual_threshold, color_distance_limit
- Fix incorrect default tolerance value in docs (0.0005 → 0.001 to match code) - Add decision tree for choosing between perceptual_threshold vs color_distance_limit - Clarify tolerance works with both color methods (not exclusive as docs claimed) - Correct color_distance_limit scale from 0-441 to 0-510 (RGBA, not RGB) - Document VIPS vs ChunkyPNG tolerance calculation difference (pixels vs bounding box) - Fix drivers.md tolerance example (0.3 → 0.001) to match recommendations - Add perceptual_threshold to YARD DSL docs (was missing) - Fix copy-paste errors in configuration.md section headings Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
1 parent b4f043c commit 9c6cc49

4 files changed

Lines changed: 78 additions & 14 deletions

File tree

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,29 @@ Then run `bundle install`.
9797

9898
**Defaults work for most Rails apps.** `blur_active_element`, `hide_caret`, and `fail_if_new` (in CI) are enabled automatically.
9999

100-
If you see inconsistent results, add tolerance:
100+
If you see inconsistent results, choose a color comparison method:
101101

102102
```ruby
103+
# Option 1: Perceptual (recommended, VIPS only)
104+
Capybara::Screenshot::Diff.perceptual_threshold = 2.0
105+
106+
# Option 2: Raw RGB tolerance (legacy)
107+
Capybara::Screenshot::Diff.tolerance = 0.0005
108+
109+
# Always set window_size for consistent dimensions
103110
Capybara::Screenshot::Diff.configure do |screenshot, diff|
104111
screenshot.window_size = [1280, 1024]
105-
diff.tolerance = 0.0005
106112
end
107113
```
108114

109-
| Use Case | VIPS `tolerance` | ChunkyPNG `color_distance_limit` |
110-
|----------|-----------------|--------------------------------|
111-
| Standard Rails apps | 0.0005 | 15 |
112-
| Animated/complex pages | 0.01 | 30 |
113-
| Pixel-perfect design | 0.0001 | 5 |
115+
| Use Case | VIPS `perceptual_threshold` | VIPS `tolerance` | ChunkyPNG `color_distance_limit` |
116+
|----------|---------------------------|-----------------|--------------------------------|
117+
| Cross-OS/browser testing | 2.0 (recommended) |||
118+
| Standard Rails apps || 0.001 (default) | 15 |
119+
| Animated/complex pages || 0.01 | 30 |
120+
| Pixel-perfect design || 0.0001 | 5 |
114121

115-
See [Configuration Reference](docs/configuration.md) for all options.
122+
**⚠️ Color methods are exclusive:** Use `perceptual_threshold` OR `color_distance_limit`, not both. But `tolerance` works with either — it's applied by default for VIPS (0.001). See [Choosing the Right Method](docs/configuration.md#choosing-the-right-color-comparison-method).
116123

117124
## Troubleshooting
118125

docs/configuration.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,53 @@ end
2626
| Use Case | VIPS `tolerance` | ChunkyPNG `color_distance_limit` | `stability_time_limit` |
2727
|----------|-----------------|--------------------------------|----------------------|
2828
| Animated/complex pages | 0.01 | 30 | 2s |
29-
| Standard Rails apps | 0.0005 | 15 | 1s |
29+
| Standard Rails apps | 0.001 (default) | 15 | 1s |
3030
| Pixel-perfect design tests | 0.0001 | 5 | 1s |
3131

32+
**Note:** VIPS defaults to `tolerance: 0.001` (allow 0.1% pixel difference). ChunkyPNG has no default tolerance.
33+
34+
## Choosing the Right Color Comparison Method
35+
36+
**Important:** `perceptual_threshold`, `color_distance_limit`, and `tolerance` serve different purposes. Use this decision tree:
37+
38+
### Step 1: Choose color comparison method (pick ONE)
39+
40+
| Method | Scale | Driver | Best for |
41+
|--------|-------|--------|----------|
42+
| `perceptual_threshold` | 0-100+ (dE00) | VIPS only | Cross-OS/browser font rendering, anti-aliasing |
43+
| `color_distance_limit` | 0-510 (RGBA Euclidean) | VIPS, ChunkyPNG | Legacy setups, fine-grained RGB control |
44+
45+
**Recommendation:** Use `perceptual_threshold: 2.0` for most cases. It matches human perception and needs less tuning.
46+
47+
**⚠️ Color comparison methods are exclusive:** `perceptual_threshold` and `color_distance_limit` cannot both be active — if you set both, `perceptual_threshold` wins and `color_distance_limit` is ignored. However, `tolerance` works with **both** methods and is applied by default for VIPS (0.001). This means even with `perceptual_threshold: 2.0`, the `tolerance: 0.001` default still filters results.
48+
49+
### Step 2: Set tolerance (optional, independent)
50+
51+
| Setting | What it does | Scale |
52+
|---------|--------------|-------|
53+
| `tolerance` | Maximum allowed *ratio* of different pixels (VIPS) or diff bounding box (ChunkyPNG) | 0.0-1.0 |
54+
55+
**Example:** `tolerance: 0.001` allows 0.1% of the image to differ (e.g., 125 pixels in a 1280×1024 screenshot).
56+
57+
**Key difference:**
58+
- `perceptual_threshold` / `color_distance_limit`**"how different can a pixel be?"**
59+
- `tolerance`**"how many pixels can differ?"**
60+
61+
**⚠️ Driver difference:** VIPS counts actual different pixels. ChunkyPNG counts the bounding box area around differences — a single pixel diff creates a box, and the entire box area counts against tolerance. This makes ChunkyPNG stricter with the same tolerance value.
62+
63+
### Quick start
64+
65+
```ruby
66+
# Modern approach (recommended)
67+
screenshot 'dashboard', perceptual_threshold: 2.0
68+
69+
# Allow small noise regions
70+
screenshot 'dashboard', perceptual_threshold: 2.0, tolerance: 0.001
71+
72+
# Legacy ChunkyPNG setup
73+
screenshot 'dashboard', color_distance_limit: 15
74+
```
75+
3276
## Configuration Tiers
3377

3478
**Tier 1 — Zero config (works immediately):**

docs/drivers.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ Capybara::Screenshot::Diff.perceptual_threshold = 2.0
2727
Use `perceptual_threshold` when you see false positives from font rendering differences across
2828
CI environments, or when `color_distance_limit` with raw RGB requires frequent tuning.
2929

30-
**Note:** `perceptual_threshold` and `color_distance_limit` are independent options on different
31-
scales. `perceptual_threshold` uses dE00 (0-100+), `color_distance_limit` uses Euclidean RGB
32-
distance (0-441). Set one or the other, not both.
30+
**⚠️ Important:** `perceptual_threshold` and `color_distance_limit` are **mutually exclusive**.
31+
If you set both, `perceptual_threshold` takes priority and `color_distance_limit` is silently ignored.
32+
33+
These options use different scales and algorithms:
34+
- `perceptual_threshold` → CIE dE00 perceptual distance (0-100+)
35+
- `color_distance_limit` → Euclidean RGBA distance (0-510)
36+
37+
**Choose one based on your driver setup:**
38+
- VIPS with `ruby-vips` gem → prefer `perceptual_threshold`
39+
- ChunkyPNG (no native dependencies) → use `color_distance_limit`
3340

3441
## Available Image Processing Drivers
3542

@@ -65,14 +72,16 @@ You can use the `tolerance` option to the `screenshot` method to set level:
6572
```ruby
6673
test 'unstable area' do
6774
visit '/'
68-
screenshot 'index', tolerance: 0.3
75+
# tolerance: 0.01 allows 1% of pixels to differ (use for noisy pages)
76+
screenshot 'index', tolerance: 0.01
6977
end
7078
```
7179

7280
You can also set this globally:
7381

7482
```ruby
75-
Capybara::Screenshot::Diff.tolerance = 0.3
83+
# Default for VIPS is 0.001 (0.1% pixel difference allowed)
84+
Capybara::Screenshot::Diff.tolerance = 0.001
7685
```
7786

7887
## Median filter size (vips only)

lib/capybara_screenshot_diff/dsl.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ def screenshot_group(name)
4141
# @option options [Array<Integer>] :crop [left, top, right, bottom] Edge coordinates to crop the screenshot to.
4242
# @option options [Array<Array<Integer>>] :skip_area Array of [left, top, right, bottom] edge coordinates to ignore.
4343
# @option options [Numeric] :tolerance (0.001 for :vips driver) Color tolerance for comparison.
44+
# Represents the maximum allowed ratio of different pixels (0.0-1.0 scale).
4445
# @option options [Numeric] :color_distance_limit Maximum allowed color distance between pixels.
46+
# Uses Euclidean RGB distance (0-441 scale). Mutually exclusive with :perceptual_threshold.
47+
# @option options [Numeric] :perceptual_threshold Maximum perceptual color difference (CIE dE00).
48+
# Uses human perception-based scale (0-100+). VIPS only. Takes priority over :color_distance_limit if both set.
4549
# @option options [Numeric] :shift_distance_limit Maximum allowed shift distance for pixels.
4650
# @option options [Numeric] :area_size_limit Maximum allowed difference area size in pixels.
4751
# @option options [Symbol] :driver (:auto) The image processing driver to use (:auto, :chunky_png, :vips).

0 commit comments

Comments
 (0)