-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
292 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/components/pages/Generator/CanvasSection/Gradient/ColorPicker/ColorPicker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {useCallback, useRef, useState} from 'react'; | ||
import {useOnClickOutside} from 'usehooks-ts'; | ||
import {rgbToHex} from '@/utils/colors'; | ||
import {type ColorRGB} from '@/types'; | ||
import {RgbColorPicker} from 'react-colorful'; | ||
|
||
type ColorPickerProps = { | ||
readonly color: ColorRGB; | ||
readonly setColor: (newColor: ColorRGB) => void; | ||
}; | ||
|
||
export function ColorPicker({color, setColor}: ColorPickerProps) { | ||
const colorHex = rgbToHex(color); | ||
|
||
const popoverRef = useRef<HTMLDivElement>(null); | ||
const [popoverOpen, setPopoverOpen] = useState<boolean>(false); | ||
const closePopover = useCallback(() => { | ||
setPopoverOpen(false); | ||
}, []); | ||
useOnClickOutside(popoverRef, closePopover); | ||
|
||
return ( | ||
<div className='flex items-center gap-2'> | ||
<div | ||
className='relative h-8 w-8 rounded-sm border border-white' | ||
style={{backgroundColor: colorHex}} | ||
onClick={() => { | ||
setPopoverOpen(true); | ||
}} | ||
> | ||
{popoverOpen && ( | ||
<div ref={popoverRef} className='absolute bottom-full left-0 z-10'> | ||
<RgbColorPicker color={color} onChange={setColor} /> | ||
</div> | ||
)} | ||
</div> | ||
<span className='w-16 text-sm text-white/75'>{colorHex}</span> | ||
</div> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
src/components/pages/Generator/CanvasSection/Gradient/ColorPicker/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {ColorPicker} from './ColorPicker'; |
104 changes: 104 additions & 0 deletions
104
src/components/pages/Generator/CanvasSection/Gradient/Gradient.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { | ||
forwardRef, | ||
useEffect, | ||
useImperativeHandle, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
import {Button} from '@/components/ui/Button'; | ||
import {rgb} from '@/utils/colors'; | ||
import {type ColorRGB} from '@/types'; | ||
import {getCtx2dFromRef} from '../utils/getCtx2dFromRef'; | ||
import {getCanvasDimensions} from '../utils/getCanvasDimensions'; | ||
import {ColorPicker} from './ColorPicker'; | ||
|
||
const colorsMin = 2; | ||
const colorsMax = 20; | ||
const colorsDefault: ColorRGB[] = [ | ||
{r: 0, g: 255, b: 255}, | ||
{r: 149, g: 0, b: 255}, | ||
{r: 255, g: 229, b: 0}, | ||
]; | ||
|
||
export const Gradient = forwardRef<HTMLCanvasElement>((_, forwardedRef) => { | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
useImperativeHandle(forwardedRef, () => { | ||
if (!canvasRef.current) { | ||
throw new TypeError('Canvas ref is not set'); | ||
} | ||
|
||
return canvasRef.current; | ||
}); | ||
|
||
const [colors, setColors] = useState<ColorRGB[]>(colorsDefault); | ||
|
||
const addColor = () => { | ||
setColors([...colors, {r: 0, g: 0, b: 0}]); | ||
}; | ||
|
||
const setColorForIndex = (index: number) => (newColor: ColorRGB) => { | ||
const newColors = [...colors]; | ||
newColors[index] = newColor; | ||
setColors(newColors); | ||
}; | ||
|
||
const deleteColorForIndex = (index: number) => () => { | ||
const newColors = [...colors]; | ||
newColors.splice(index, 1); | ||
setColors(newColors); | ||
}; | ||
|
||
useEffect(() => { | ||
const ctx2d = getCtx2dFromRef(canvasRef); | ||
const {w, h} = getCanvasDimensions(ctx2d); | ||
|
||
ctx2d.clearRect(0, 0, w, h); | ||
|
||
const gradient = ctx2d.createLinearGradient(0, 0, w, 0); | ||
for (let i = 0; i < colors.length; i++) { | ||
gradient.addColorStop(i / Math.max(colors.length - 1, 1), rgb(colors[i])); | ||
} | ||
|
||
ctx2d.fillStyle = gradient; | ||
ctx2d.fillRect(0, 0, w, h); | ||
}, [colors]); | ||
|
||
return ( | ||
<div> | ||
<h2>Gradient</h2> | ||
<div className='flex flex-col gap-2 sm:flex-row'> | ||
<div className='pt-1'> | ||
<canvas | ||
ref={canvasRef} | ||
className='h-36 w-36 border border-dashed border-white' | ||
width={256} | ||
height={256} | ||
/> | ||
</div> | ||
<div> | ||
<div className='flex flex-col gap-1 pt-1'> | ||
{colors.map((color, index) => ( | ||
<div | ||
key={index} // eslint-disable-line react/no-array-index-key | ||
className='flex gap-2' | ||
> | ||
<ColorPicker color={color} setColor={setColorForIndex(index)} /> | ||
<Button | ||
disabled={colors.length <= colorsMin} | ||
onClick={deleteColorForIndex(index)} | ||
> | ||
Delete | ||
</Button> | ||
</div> | ||
))} | ||
</div> | ||
<div className='pt-2'> | ||
<Button disabled={colors.length >= colorsMax} onClick={addColor}> | ||
Add stop | ||
</Button> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {Gradient} from './Gradient'; |
35 changes: 35 additions & 0 deletions
35
src/components/pages/Generator/CanvasSection/utils/drawColor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {getCanvasDimensions} from './getCanvasDimensions'; | ||
|
||
export const drawColor = ({ | ||
ctx2d, | ||
ctx2dGradient, | ||
}: { | ||
ctx2d: CanvasRenderingContext2D; | ||
ctx2dGradient: CanvasRenderingContext2D; | ||
}): void => { | ||
const {w, h} = getCanvasDimensions(ctx2d); | ||
const {w: wGradient} = getCanvasDimensions(ctx2dGradient); | ||
|
||
const source = ctx2d.getImageData(0, 0, w, h); | ||
const sourceGradient = ctx2dGradient.getImageData(0, 0, wGradient, 1); | ||
const destination = ctx2d.createImageData(w, h); | ||
|
||
const paletteR: number[] = []; | ||
const paletteG: number[] = []; | ||
const paletteB: number[] = []; | ||
|
||
for (let i = 0; i < wGradient * 4; i += 4) { | ||
paletteR.push(sourceGradient.data[i]); | ||
paletteG.push(sourceGradient.data[i + 1]); | ||
paletteB.push(sourceGradient.data[i + 2]); | ||
} | ||
|
||
for (let i = 0; i < source.data.length; i += 4) { | ||
destination.data[i] = paletteR[source.data[i]]; | ||
destination.data[i + 1] = paletteG[source.data[i + 1]]; | ||
destination.data[i + 2] = paletteB[source.data[i + 2]]; | ||
destination.data[i + 3] = source.data[i + 3]; | ||
} | ||
|
||
ctx2d.putImageData(destination, 0, 0); | ||
}; |
Oops, something went wrong.