MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / _applyDither

Method _applyDither

src/tools/texture-lab/renderer.js:1346–1365  ·  view source on GitHub ↗
(data, width, height, params)

Source from the content-addressed store, hash-verified

1344 }
1345
1346 _applyDither(data, width, height, params) {
1347 const levels = params.levels;
1348 const threshold = params.threshold;
1349 const methodSize =
1350 params.method === DITHER_METHODS.BAYER_2X2 ? 2 : params.method === DITHER_METHODS.BAYER_4X4 ? 4 : 8;
1351
1352 for (let i = 0; i < data.length; i += 4) {
1353 const x = (i / 4) % width;
1354 const y = Math.floor(i / 4 / width);
1355
1356 const bayer = this._getBayerValue(x, y, methodSize) - 0.5;
1357 const spread = threshold / levels;
1358
1359 for (let c = 0; c < 3; c++) {
1360 const val = data[i + c] / 255 + bayer * spread;
1361 const quantized = Math.round(val * (levels - 1)) / (levels - 1);
1362 data[i + c] = Math.max(0, Math.min(255, quantized * 255));
1363 }
1364 }
1365 }
1366
1367 _applyHalftone(data, width, height, params) {
1368 const dotSize = params.dotSize;

Callers 1

renderMethod · 0.95

Calls 1

_getBayerValueMethod · 0.95

Tested by

no test coverage detected