feat: add instruments registry

This commit is contained in:
2026-08-22 10:06:49 +05:00
parent 9ccc9f94ac
commit 719bda2e22
4 changed files with 368 additions and 2 deletions
+18 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { parseHex, removeColorToAlpha } from './alpha';
import { flattenOntoColor, parseHex, removeColorToAlpha } from './alpha';
import { makeImage } from './test-helpers';
describe('removeColorToAlpha', () => {
@@ -55,6 +55,23 @@ describe('removeColorToAlpha', () => {
});
});
describe('flattenOntoColor', () => {
it('непрозрачный пиксель не меняется, альфа становится 255', () => {
const out = flattenOntoColor(makeImage(1, 1, [[10, 20, 30, 255]]), '#ffffff');
expect([...out.data]).toEqual([10, 20, 30, 255]);
});
it('полностью прозрачный пиксель становится цветом подложки', () => {
const out = flattenOntoColor(makeImage(1, 1, [[99, 99, 99, 0]]), '#ff8040');
expect([...out.data]).toEqual([255, 128, 64, 255]);
});
it('полупрозрачный пиксель смешивается с подложкой', () => {
const out = flattenOntoColor(makeImage(1, 1, [[10, 20, 30, 128]]), '#ffffff');
expect([...out.data]).toEqual([132, 137, 142, 255]);
});
});
describe('parseHex', () => {
it('разбирает #rrggbb, rrggbb, #rgb', () => {
expect(parseHex('#ff8040')).toEqual([255, 128, 64]);
+15 -1
View File
@@ -1,4 +1,4 @@
import type { PixelImage } from './types';
import { createPixelImage, type PixelImage } from './types';
const MAX_COLOR_DISTANCE = Math.sqrt(3 * 255 * 255);
@@ -22,6 +22,20 @@ export function removeColorToAlpha(
return out;
}
export function flattenOntoColor(img: PixelImage, hex: string): PixelImage {
const [bgR, bgG, bgB] = parseHex(hex);
const out = createPixelImage(img.width, img.height);
for (let i = 0; i < out.data.length; i += 4) {
const a = img.data[i + 3] / 255;
const inv = 1 - a;
out.data[i] = img.data[i] * a + bgR * inv;
out.data[i + 1] = img.data[i + 1] * a + bgG * inv;
out.data[i + 2] = img.data[i + 2] * a + bgB * inv;
out.data[i + 3] = 255;
}
return out;
}
export function parseHex(hex: string): [number, number, number] {
const match = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(hex.trim());
if (!match) {