-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
0893178
commit 71c7397
Showing
6 changed files
with
185 additions
and
82 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
morpheus-client/components/ColorPaletteGenerator/ColorPaletteGenerator.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,100 @@ | ||
import { useRef, useState } from "react"; | ||
import { CloseIcon } from "@/components/icons/close"; | ||
import { CheckIcon } from "@/components/icons/check"; | ||
import { useImagine } from "@/context/ImagineContext"; | ||
import { useToastContext } from "@/context/ToastContext"; | ||
|
||
type ColorPaletteGeneratorProps = { | ||
closeModal: () => void; | ||
}; | ||
|
||
const initialColors = ["#ffffff", "#ffffff"]; | ||
|
||
const ColorPaletteGenerator = (props: ColorPaletteGeneratorProps) => { | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
const { setColorPaletteFile } = useImagine(); | ||
const { showInfoAlert } = useToastContext(); | ||
const [colors, setColors] = useState<string[]>(initialColors); | ||
|
||
const addColor = () => { | ||
if (colors.length >= 5) { | ||
showInfoAlert("You can only have 5 colors in your palette"); | ||
return; | ||
} | ||
setColors([...colors, "#ffffff"]); | ||
}; | ||
|
||
const changeColor = (color: string, index: number) => { | ||
const newColors = [...colors]; | ||
newColors[index] = color; | ||
setColors(newColors); | ||
}; | ||
|
||
const resetColors = () => { | ||
setColors(initialColors); | ||
}; | ||
|
||
const handleCompleted = () => { | ||
if (!canvasRef) return; | ||
const canvas = canvasRef.current; | ||
if (!canvas) return; | ||
const context = canvas.getContext("2d"); | ||
if (!context) return; | ||
|
||
const gradient = context.createLinearGradient(0, 0, 0, canvas.height); | ||
const step = 1 / (colors.length - 1); | ||
for (let i = 0; i < colors.length; i++) { | ||
gradient.addColorStop(step * i, colors[i]); | ||
} | ||
context.fillStyle = gradient; | ||
context.fillRect(0, 0, canvas.width, canvas.height); | ||
|
||
canvas.toBlob((blob: any) => { | ||
const file = new File([blob as Blob], "color-palette.png", { | ||
type: "image/png", | ||
}); | ||
setColorPaletteFile(file); | ||
props.closeModal(); | ||
}, "image/png"); | ||
}; | ||
|
||
return ( | ||
<div className=""> | ||
<h2 className="headline-3 white">Generate Color Palette</h2> | ||
|
||
<canvas ref={canvasRef} width={500} height={500} hidden /> | ||
<div className="w-[500px] h-[500px] grid mt-5 p-1 bg-white"> | ||
{colors.map((color, index) => ( | ||
<div key={index}> | ||
<input | ||
type="color" | ||
value={color} | ||
className="w-full h-full" | ||
onChange={(e) => changeColor(e.target.value, index)} | ||
/> | ||
</div> | ||
))} | ||
</div> | ||
|
||
<div className="flex flex-row items-center gap-[16px] mt-10"> | ||
<button className="buttonSubmit w-full" onClick={addColor}> | ||
Add Color | ||
</button> | ||
|
||
<span className="material-icons text-white" onClick={resetColors}> | ||
replay | ||
</span> | ||
|
||
<span onClick={props.closeModal}> | ||
<CloseIcon width={"24"} height={"24"} color={"#FFFFFF"} /> | ||
</span> | ||
|
||
<span onClick={handleCompleted}> | ||
<CheckIcon width={"24"} height={"24"} color={"#FFFFFF"} /> | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ColorPaletteGenerator; |
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
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
Oops, something went wrong.