mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
refactor: rename Tool.alpha to Tool.experimental
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { Sharp, BrightnessOptions } from "../types.js";
|
||||
import type { BrightnessOptions, Sharp } from "../types.js";
|
||||
|
||||
export async function brightness(image: Sharp, options: BrightnessOptions): Promise<Sharp> {
|
||||
const { value } = options;
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import type { Sharp, ColorChannelOptions } from "../types.js";
|
||||
import type { ColorChannelOptions, Sharp } from "../types.js";
|
||||
|
||||
export async function colorChannels(
|
||||
image: Sharp,
|
||||
options: ColorChannelOptions
|
||||
): Promise<Sharp> {
|
||||
export async function colorChannels(image: Sharp, options: ColorChannelOptions): Promise<Sharp> {
|
||||
const { red, green, blue } = options;
|
||||
|
||||
if (red < 0 || red > 200) {
|
||||
@@ -21,11 +18,7 @@ export async function colorChannels(
|
||||
const gMul = green / 100;
|
||||
const bMul = blue / 100;
|
||||
|
||||
const matrix: [
|
||||
[number, number, number],
|
||||
[number, number, number],
|
||||
[number, number, number],
|
||||
] = [
|
||||
const matrix: [[number, number, number], [number, number, number], [number, number, number]] = [
|
||||
[rMul, 0, 0],
|
||||
[0, gMul, 0],
|
||||
[0, 0, bMul],
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import sharp from "sharp";
|
||||
import type { Sharp, CompressOptions, OutputFormat } from "../types.js";
|
||||
import type { CompressOptions, OutputFormat, Sharp } from "../types.js";
|
||||
|
||||
const FORMAT_MAP: Record<OutputFormat, string> = {
|
||||
jpg: "jpeg",
|
||||
@@ -16,7 +16,7 @@ export async function compress(image: Sharp, options: CompressOptions): Promise<
|
||||
const metadata = await image.metadata();
|
||||
const outputFormat = format
|
||||
? (FORMAT_MAP[format] as keyof import("sharp").FormatEnum)
|
||||
: (metadata.format as keyof import("sharp").FormatEnum) ?? "jpeg";
|
||||
: ((metadata.format as keyof import("sharp").FormatEnum) ?? "jpeg");
|
||||
|
||||
if (targetSizeBytes !== undefined) {
|
||||
if (targetSizeBytes <= 0) {
|
||||
@@ -37,7 +37,7 @@ export async function compress(image: Sharp, options: CompressOptions): Promise<
|
||||
async function compressToTargetSize(
|
||||
inputBuffer: Buffer,
|
||||
format: keyof import("sharp").FormatEnum,
|
||||
targetBytes: number
|
||||
targetBytes: number,
|
||||
): Promise<Sharp> {
|
||||
let low = 1;
|
||||
let high = 100;
|
||||
@@ -69,9 +69,7 @@ async function compressToTargetSize(
|
||||
|
||||
// If we never found a suitable buffer, compress at lowest quality found
|
||||
if (bestBuffer === null) {
|
||||
bestBuffer = await sharp(inputBuffer)
|
||||
.toFormat(format, { quality: bestQuality })
|
||||
.toBuffer();
|
||||
bestBuffer = await sharp(inputBuffer).toFormat(format, { quality: bestQuality }).toBuffer();
|
||||
}
|
||||
|
||||
// Preserve format + quality so the caller's .toBuffer() doesn't re-encode at defaults
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Sharp, ContrastOptions } from "../types.js";
|
||||
import type { ContrastOptions, Sharp } from "../types.js";
|
||||
|
||||
export async function contrast(image: Sharp, options: ContrastOptions): Promise<Sharp> {
|
||||
const { value } = options;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Sharp, ConvertOptions, OutputFormat } from "../types.js";
|
||||
import type { ConvertOptions, OutputFormat, Sharp } from "../types.js";
|
||||
|
||||
const FORMAT_MAP: Record<OutputFormat, string> = {
|
||||
jpg: "jpeg",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Sharp, CropOptions } from "../types.js";
|
||||
import type { CropOptions, Sharp } from "../types.js";
|
||||
|
||||
export async function crop(image: Sharp, options: CropOptions): Promise<Sharp> {
|
||||
const { left, top, width, height } = options;
|
||||
@@ -16,12 +16,12 @@ export async function crop(image: Sharp, options: CropOptions): Promise<Sharp> {
|
||||
|
||||
if (left + width > imgWidth) {
|
||||
throw new Error(
|
||||
`Crop region exceeds image width: left(${left}) + width(${width}) > ${imgWidth}`
|
||||
`Crop region exceeds image width: left(${left}) + width(${width}) > ${imgWidth}`,
|
||||
);
|
||||
}
|
||||
if (top + height > imgHeight) {
|
||||
throw new Error(
|
||||
`Crop region exceeds image height: top(${top}) + height(${height}) > ${imgHeight}`
|
||||
`Crop region exceeds image height: top(${top}) + height(${height}) > ${imgHeight}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Sharp, FlipOptions } from "../types.js";
|
||||
import type { FlipOptions, Sharp } from "../types.js";
|
||||
|
||||
export async function flip(image: Sharp, options: FlipOptions): Promise<Sharp> {
|
||||
const { horizontal, vertical } = options;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Sharp, ResizeOptions } from "../types.js";
|
||||
import type { ResizeOptions, Sharp } from "../types.js";
|
||||
|
||||
export async function resize(image: Sharp, options: ResizeOptions): Promise<Sharp> {
|
||||
let { width, height, fit, withoutEnlargement, percentage } = options;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Sharp, RotateOptions } from "../types.js";
|
||||
import type { RotateOptions, Sharp } from "../types.js";
|
||||
|
||||
export async function rotate(image: Sharp, options: RotateOptions): Promise<Sharp> {
|
||||
const { angle, background } = options;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Sharp, SaturationOptions } from "../types.js";
|
||||
import type { SaturationOptions, Sharp } from "../types.js";
|
||||
|
||||
export async function saturation(image: Sharp, options: SaturationOptions): Promise<Sharp> {
|
||||
const { value } = options;
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
import type { Sharp } from "../types.js";
|
||||
|
||||
// Standard sepia tone matrix
|
||||
const SEPIA_MATRIX: [
|
||||
[number, number, number],
|
||||
[number, number, number],
|
||||
[number, number, number],
|
||||
] = [
|
||||
[0.393, 0.769, 0.189],
|
||||
[0.349, 0.686, 0.168],
|
||||
[0.272, 0.534, 0.131],
|
||||
];
|
||||
const SEPIA_MATRIX: [[number, number, number], [number, number, number], [number, number, number]] =
|
||||
[
|
||||
[0.393, 0.769, 0.189],
|
||||
[0.349, 0.686, 0.168],
|
||||
[0.272, 0.534, 0.131],
|
||||
];
|
||||
|
||||
export async function sepia(image: Sharp): Promise<Sharp> {
|
||||
return image.recomb(SEPIA_MATRIX);
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { Sharp, StripMetadataOptions } from "../types.js";
|
||||
|
||||
export async function stripMetadata(
|
||||
image: Sharp,
|
||||
options: StripMetadataOptions = {}
|
||||
options: StripMetadataOptions = {},
|
||||
): Promise<Sharp> {
|
||||
const { stripExif, stripGps, stripIcc, stripXmp, stripAll } = options;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user