style: fix formatting via prettier
This commit is contained in:
+42
-16
@@ -1,17 +1,22 @@
|
||||
import { createPixelImage, type PixelImage } from './types';
|
||||
import { ToolError } from './errors';
|
||||
import { createPixelImage, type PixelImage } from "./types";
|
||||
import { ToolError } from "./errors";
|
||||
|
||||
const MAX_COLOR_DISTANCE = Math.sqrt(3 * 255 * 255);
|
||||
|
||||
export function removeColorToAlpha(
|
||||
img: PixelImage,
|
||||
hex: string,
|
||||
tolerancePercent = 0
|
||||
tolerancePercent = 0,
|
||||
): PixelImage {
|
||||
const [targetR, targetG, targetB] = parseHex(hex);
|
||||
const tolerance = (clamp(tolerancePercent, 0, 100) / 100) * MAX_COLOR_DISTANCE;
|
||||
const tolerance =
|
||||
(clamp(tolerancePercent, 0, 100) / 100) * MAX_COLOR_DISTANCE;
|
||||
const thresholdSq = tolerance * tolerance;
|
||||
const out: PixelImage = { width: img.width, height: img.height, data: img.data.slice() };
|
||||
const out: PixelImage = {
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
data: img.data.slice(),
|
||||
};
|
||||
for (let i = 0; i < out.data.length; i += 4) {
|
||||
const dr = out.data[i] - targetR;
|
||||
const dg = out.data[i + 1] - targetG;
|
||||
@@ -25,7 +30,11 @@ export function removeColorToAlpha(
|
||||
|
||||
export function setAlphaChannel(img: PixelImage, percent: number): PixelImage {
|
||||
const alpha = Math.round((clamp(percent, 0, 100) / 100) * 255);
|
||||
const out: PixelImage = { width: img.width, height: img.height, data: img.data.slice() };
|
||||
const out: PixelImage = {
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
data: img.data.slice(),
|
||||
};
|
||||
for (let i = 3; i < out.data.length; i += 4) {
|
||||
out.data[i] = alpha;
|
||||
}
|
||||
@@ -44,10 +53,19 @@ export function extractAlphaMask(img: PixelImage): PixelImage {
|
||||
return out;
|
||||
}
|
||||
|
||||
export function roundCorners(img: PixelImage, radiusPercent: number): PixelImage {
|
||||
const radius = (clamp(radiusPercent, 0, 50) / 100) * (Math.min(img.width, img.height) / 2);
|
||||
if (radius < 1) return { width: img.width, height: img.height, data: img.data.slice() };
|
||||
const out: PixelImage = { width: img.width, height: img.height, data: img.data.slice() };
|
||||
export function roundCorners(
|
||||
img: PixelImage,
|
||||
radiusPercent: number,
|
||||
): PixelImage {
|
||||
const radius =
|
||||
(clamp(radiusPercent, 0, 50) / 100) * (Math.min(img.width, img.height) / 2);
|
||||
if (radius < 1)
|
||||
return { width: img.width, height: img.height, data: img.data.slice() };
|
||||
const out: PixelImage = {
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
data: img.data.slice(),
|
||||
};
|
||||
const r2 = radius * radius;
|
||||
for (let y = 0; y < out.height; y++) {
|
||||
for (let x = 0; x < out.width; x++) {
|
||||
@@ -74,7 +92,10 @@ export function invertAlpha(img: PixelImage): PixelImage {
|
||||
return out;
|
||||
}
|
||||
|
||||
export function hardenAlpha(img: PixelImage, thresholdPercent: number): PixelImage {
|
||||
export function hardenAlpha(
|
||||
img: PixelImage,
|
||||
thresholdPercent: number,
|
||||
): PixelImage {
|
||||
const threshold = (clamp(thresholdPercent, 0, 100) / 100) * 255;
|
||||
const out = createPixelImage(img.width, img.height);
|
||||
for (let i = 0; i < out.data.length; i += 4) {
|
||||
@@ -86,9 +107,14 @@ export function hardenAlpha(img: PixelImage, thresholdPercent: number): PixelIma
|
||||
return out;
|
||||
}
|
||||
|
||||
export function colorMask(img: PixelImage, hex: string, tolerancePercent = 0): PixelImage {
|
||||
export function colorMask(
|
||||
img: PixelImage,
|
||||
hex: string,
|
||||
tolerancePercent = 0,
|
||||
): PixelImage {
|
||||
const [targetR, targetG, targetB] = parseHex(hex);
|
||||
const tolerance = (clamp(tolerancePercent, 0, 100) / 100) * MAX_COLOR_DISTANCE;
|
||||
const tolerance =
|
||||
(clamp(tolerancePercent, 0, 100) / 100) * MAX_COLOR_DISTANCE;
|
||||
const thresholdSq = tolerance * tolerance;
|
||||
const out = createPixelImage(img.width, img.height);
|
||||
for (let i = 0; i < out.data.length; i += 4) {
|
||||
@@ -121,20 +147,20 @@ export function flattenOntoColor(img: PixelImage, hex: string): PixelImage {
|
||||
export function parseHex(hex: string): [number, number, number] {
|
||||
const match = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(hex.trim());
|
||||
if (!match) {
|
||||
throw new ToolError('errors.badHex', { value: hex });
|
||||
throw new ToolError("errors.badHex", { value: hex });
|
||||
}
|
||||
const digits = match[1];
|
||||
if (digits.length === 3) {
|
||||
return [
|
||||
parseInt(digits[0] + digits[0], 16),
|
||||
parseInt(digits[1] + digits[1], 16),
|
||||
parseInt(digits[2] + digits[2], 16)
|
||||
parseInt(digits[2] + digits[2], 16),
|
||||
];
|
||||
}
|
||||
return [
|
||||
parseInt(digits.slice(0, 2), 16),
|
||||
parseInt(digits.slice(2, 4), 16),
|
||||
parseInt(digits.slice(4, 6), 16)
|
||||
parseInt(digits.slice(4, 6), 16),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user