-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(CssFilters): Add tests and fix clamping
- Loading branch information
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
Sources/Rendering/Core/ColorTransferFunction/test/testCssFilters.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import test from 'tape'; | ||
|
||
import * as CssFilters from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction/CssFilters'; | ||
|
||
test('Test CssFilters identity', (t) => { | ||
const color = [Math.random(), Math.random(), -1, 2]; | ||
const identity = CssFilters.createIdentityFilter(); | ||
const output = CssFilters.applyFilter(identity, ...color); | ||
t.deepEqual(color, output, 'Apply identity filter'); | ||
t.end(); | ||
}); | ||
|
||
test('Test CssFilters brightness', (t) => { | ||
const color = [0.1, 0.5, 0.9, 0.2]; | ||
const halfBright = CssFilters.createBrightnessFilter(0.5); | ||
const output = CssFilters.applyFilter(halfBright, ...color); | ||
t.deepEqual([0.05, 0.25, 0.45, 0.2], output, 'Apply brightness filter'); | ||
t.end(); | ||
}); | ||
|
||
test('Fuzzy test all CssFilters', (t) => { | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = 1; | ||
canvas.height = 1; | ||
const ctx = canvas.getContext('2d', { willReadFrequently: true }); | ||
|
||
const randomChannel = () => Math.floor(Math.random() * 256); | ||
|
||
const numberOfRepetitions = 1000; | ||
for (let repetition = 0; repetition < numberOfRepetitions; repetition++) { | ||
const contrast = 2 * Math.random(); | ||
const saturate = 2 * Math.random(); | ||
const brightness = 2 * Math.random(); | ||
const invert = Math.random(); | ||
|
||
const color = [randomChannel(), randomChannel(), randomChannel(), 255]; | ||
|
||
// Apply filters one by one (don't combine as it is not equivalent) | ||
let filtersOutput = color.map((channel) => channel / 255); | ||
const contrastFilter = CssFilters.createContrastFilter(contrast); | ||
filtersOutput = CssFilters.applyFilter(contrastFilter, ...filtersOutput); | ||
const saturateFilter = CssFilters.createSaturateFilter(saturate); | ||
filtersOutput = CssFilters.applyFilter(saturateFilter, ...filtersOutput); | ||
const brightnessFilter = CssFilters.createBrightnessFilter(brightness); | ||
filtersOutput = CssFilters.applyFilter(brightnessFilter, ...filtersOutput); | ||
const invertFilter = CssFilters.createInvertFilter(invert); | ||
filtersOutput = CssFilters.applyFilter(invertFilter, ...filtersOutput); | ||
|
||
// Reference: canvas 2D context filters | ||
ctx.fillStyle = `rgb(${color[0]} ${color[1]} ${color[2]})`; | ||
ctx.filter = `contrast(${contrast}) saturate(${saturate}) brightness(${brightness}) invert(${invert})`; | ||
ctx.beginPath(); | ||
ctx.fillRect(0, 0, canvas.width, canvas.height); | ||
const canvasPixel = ctx.getImageData(0, 0, 1, 1).data; | ||
const referenceOutput = [...canvasPixel].map((x) => x / 255); | ||
|
||
for (let channel = 0; channel < 4; ++channel) { | ||
const error = referenceOutput[channel] - filtersOutput[channel]; | ||
// The error depends on the values of each filter | ||
// For example, using a color of [127.5, 127.5, 128.49] with a saturate of 1000 creates big errors | ||
t.assert(Math.abs(error) <= 7 / 255); | ||
} | ||
} | ||
t.end(); | ||
}); |