Files
omni-tools/src/pages/tools/image/png/create-transparent/index.tsx

128 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-06-25 07:44:36 +01:00
import { Box } from '@mui/material';
2024-06-26 07:47:17 +01:00
import React, { useState } from 'react';
2024-06-25 07:44:36 +01:00
import * as Yup from 'yup';
2025-02-23 01:38:42 +01:00
import ToolFileInput from '@components/input/ToolFileInput';
import ToolFileResult from '@components/result/ToolFileResult';
import ToolOptions from '@components/options/ToolOptions';
import ColorSelector from '@components/options/ColorSelector';
2024-06-25 07:44:36 +01:00
import Color from 'color';
import TextFieldWithDesc from 'components/options/TextFieldWithDesc';
2025-02-23 01:38:42 +01:00
import ToolInputAndResult from '@components/ToolInputAndResult';
import { areColorsSimilar } from 'utils/color';
2024-06-25 07:44:36 +01:00
const initialValues = {
fromColor: 'white',
similarity: '10'
};
const validationSchema = Yup.object({
// splitSeparator: Yup.string().required('The separator is required')
});
export default function ChangeColorsInPng() {
const [input, setInput] = useState<File | null>(null);
const [result, setResult] = useState<File | null>(null);
2024-06-26 07:47:17 +01:00
const compute = (optionsValues: typeof initialValues, input: any) => {
2024-06-27 21:52:41 +01:00
if (!input) return;
2024-06-26 07:47:17 +01:00
const { fromColor, similarity } = optionsValues;
2024-06-25 07:44:36 +01:00
2024-06-26 07:47:17 +01:00
let fromRgb: [number, number, number];
try {
//@ts-ignore
fromRgb = Color(fromColor).rgb().array();
} catch (err) {
return;
}
const processImage = async (
file: File,
fromColor: [number, number, number],
similarity: number
) => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (ctx == null) return;
const img = new Image();
2024-06-25 07:44:36 +01:00
2024-06-26 07:47:17 +01:00
img.src = URL.createObjectURL(file);
await img.decode();
2024-06-25 07:44:36 +01:00
2024-06-26 07:47:17 +01:00
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
2024-06-25 07:44:36 +01:00
2024-06-26 07:47:17 +01:00
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data: Uint8ClampedArray = imageData.data;
2024-06-25 07:44:36 +01:00
2024-06-26 07:47:17 +01:00
for (let i = 0; i < data.length; i += 4) {
const currentColor: [number, number, number] = [
data[i],
data[i + 1],
data[i + 2]
];
if (areColorsSimilar(currentColor, fromColor, similarity)) {
2024-06-26 07:47:17 +01:00
data[i + 3] = 0; // Set alpha to 0 (transparent)
2024-06-25 07:44:36 +01:00
}
2024-06-26 07:47:17 +01:00
}
2024-06-25 07:44:36 +01:00
2024-06-26 07:47:17 +01:00
ctx.putImageData(imageData, 0, 0);
2024-06-25 07:44:36 +01:00
2024-06-26 07:47:17 +01:00
canvas.toBlob((blob) => {
if (blob) {
const newFile = new File([blob], file.name, { type: 'image/png' });
setResult(newFile);
}
}, 'image/png');
};
2024-06-25 07:44:36 +01:00
2024-06-26 07:47:17 +01:00
processImage(input, fromRgb, Number(similarity));
2024-06-25 07:44:36 +01:00
};
return (
<Box>
<ToolInputAndResult
input={
<ToolFileInput
value={input}
onChange={setInput}
accept={['image/png']}
title={'Input PNG'}
/>
}
result={
<ToolFileResult
title={'Transparent PNG'}
value={result}
extension={'png'}
/>
}
/>
2024-06-26 07:47:17 +01:00
<ToolOptions
compute={compute}
2024-06-27 21:52:41 +01:00
getGroups={({ values, updateField }) => [
2024-06-26 07:47:17 +01:00
{
title: 'From color and similarity',
component: (
<Box>
<ColorSelector
value={values.fromColor}
2024-06-28 14:26:33 +02:00
onColorChange={(val) => updateField('fromColor', val)}
2024-06-26 07:47:17 +01:00
description={'Replace this color (from color)'}
2024-06-28 14:58:29 +02:00
inputProps={{ 'data-testid': 'color-input' }}
2024-06-26 07:47:17 +01:00
/>
<TextFieldWithDesc
value={values.similarity}
2024-06-27 21:52:41 +01:00
onOwnChange={(val) => updateField('similarity', val)}
2024-06-26 07:47:17 +01:00
description={
'Match this % of similar colors of the from color. For example, 10% white will match white and a little bit of gray.'
2024-06-25 07:44:36 +01:00
}
2024-06-26 07:47:17 +01:00
/>
</Box>
)
}
]}
initialValues={initialValues}
input={input}
/>
2024-06-25 07:44:36 +01:00
</Box>
);
}