Skip to content

Commit

Permalink
limited palette
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Aug 20, 2024
1 parent c863aa8 commit 1bc3da6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 55 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/dither/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"colorjs.io": "^0.5.2",
"comlink": "^4.4.1",
"framer-plugin": "^0.3.1",
"ogl": "^1.0.8",
Expand Down
17 changes: 13 additions & 4 deletions plugins/dither/src/materials/ordered.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,17 @@ export class OrderedDitherMaterial extends Program {
// }
} else if (uColorMode == 2) { // Custom Palette
// color.rgb = vec3(luma(rgb));
color.rgb = vec3(luma(rgb));
threshold -= 0.44; // arbitraty threshold adjustment
ivec2 paletteTextureSize = textureSize(uDitherTexture, 0);
color.rgb = texture(uPaletteTexture, vec2(quantize(1. - (luma(rgb) + threshold), paletteTextureSize.x), 0.0)).rgb;
// color.r = texture(uPaletteTexture, vec2(quantize(1. - (color.r + threshold), paletteTextureSize.x), 0.0)).r;
// color.g = texture(uPaletteTexture, vec2(quantize(1. - (color.g + threshold), paletteTextureSize.x), 0.0)).g;
// color.b = texture(uPaletteTexture, vec2(quantize(1. - (color.b + threshold), paletteTextureSize.x), 0.0)).b;
// if(color.r >= threshold) {
// if(luma(rgb) >= threshold) {
// float x = quantize(color.r + threshold, uQuantization);
Expand Down Expand Up @@ -154,7 +159,11 @@ export class OrderedDitherMaterial extends Program {
fragColor = vec4(orderedDither(color.rgb, pixelizedUv), color.a);
// fragColor = texture(uPaletteTexture, vUv);
if(vUv.y >= 0.9) {
fragColor = texture(uPaletteTexture, vUv);
}
// fragColor = color;
}
Expand Down Expand Up @@ -219,7 +228,7 @@ export const OrderedDither = forwardRef(function RandomDither(
// const [ditherPixelSize, setDitherPixelSize] = useState(1)

const { texture: ditherTexture, canvas } = useOrderedDitheringTexture(gl, ORDERED_DITHERING_MATRICES[mode])
const { texture: paletteTexture } = useGradientTexture(gl, colors)
const { texture: paletteTexture } = useGradientTexture(gl, colors, quantization)

useEffect(() => {
// document.body.appendChild(canvas)
Expand Down Expand Up @@ -314,7 +323,7 @@ export const OrderedDither = forwardRef(function RandomDither(
<option value="2">Custom Palette</option>
</select>
</div>
{[0, 1].includes(colorMode) && (
{[0, 1, 2].includes(colorMode) && (
<div className="gui-row">
<label className="gui-label">Quantization</label>
<input
Expand All @@ -330,7 +339,7 @@ export const OrderedDither = forwardRef(function RandomDither(
)}
{[2].includes(colorMode) && (
<div className="gui-row">
<label className="gui-label">Color Palette</label>
<label className="gui-label">Palette</label>
<Palette
onChange={colors => {
setColors(colors)
Expand Down
45 changes: 2 additions & 43 deletions plugins/dither/src/palette.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from "react"

const DEFAULT_COLORS = ["#ccff33", "#9ef01a", "#70e000", "#38b000", "#008000", "#006400", "#004b23"]
// const DEFAULT_COLORS = ["#ffffff", "#E30613"]
const DEFAULT_COLORS = ["#E30613", "#000000"]

export function Palette({ onChange }: { onChange?: (colors: string[]) => void }) {
const [colors, setColors] = useState(DEFAULT_COLORS)
Expand All @@ -26,50 +27,8 @@ export function Palette({ onChange }: { onChange?: (colors: string[]) => void })
})
}}
/>
{colors.length > 2 && (
<div className="controls">
<div
onClick={() =>
setColors(() => {
const newColors = [...colors]
newColors.splice(i, 1)
return newColors
})
}
>
<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8">
<path
d="M 1.5 6.5 L 6.5 1.5"
fill="transparent"
stroke-width="1.5"
stroke="currentColor"
stroke-linecap="round"
></path>
<path
d="M 6.5 6.5 L 1.5 1.5"
fill="transparent"
stroke-width="1.5"
stroke="currentColor"
stroke-linecap="round"
></path>
</svg>
</div>
</div>
)}
</div>
))}
<div
onClick={() =>
setColors(() => {
const newColors = [...colors]
newColors.push(DEFAULT_COLORS[newColors.length])
return newColors
})
}
className="add"
>
Add Color
</div>
</div>
)
}
21 changes: 13 additions & 8 deletions plugins/dither/src/use-gradient-texture.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import { useEffect, useState } from "react"
import { OGLRenderingContext, Texture, Color } from "ogl"
import { OGLRenderingContext, Texture } from "ogl"
import Color from "colorjs.io"

export function useGradientTexture(gl: OGLRenderingContext, colors: string[]) {
export function useGradientTexture(gl: OGLRenderingContext, colors: string[], quantization: number) {
const [texture] = useState(() => new Texture(gl, { minFilter: gl.NEAREST, magFilter: gl.NEAREST }))
const [canvas] = useState(() => document.createElement("canvas"))

useEffect(() => {
if (!colors.length) return

const pixels = new Uint8ClampedArray(colors.length * 4)
quantization = Math.max(2, quantization)

const list = new Color(colors[0]).steps(colors[1], { steps: quantization, space: "hsl", outputSpace: "srgb" })

const pixels = new Uint8ClampedArray(list.length * 4)
for (let i = 0; i < pixels.length; i += 4) {
const color = new Color(colors[i / 4])
const color = list[i / 4]
pixels[i] = color.r * 255
pixels[i + 1] = color.g * 255
pixels[i + 2] = color.b * 255
pixels[i + 3] = 255
}

canvas.width = colors.length
canvas.width = list.length
canvas.height = 1
const ctx = canvas.getContext("2d")
// ctx.putImageData(pixels, 0, 0)
ctx?.putImageData(new ImageData(pixels, colors.length, 1), 0, 0)
ctx?.putImageData(new ImageData(pixels, list.length, 1), 0, 0)

texture.image = pixels
texture.width = colors.length
texture.width = list.length
texture.height = 1
texture.update()
}, [texture, canvas, colors])
}, [texture, canvas, colors, quantization])

return { texture }
}

0 comments on commit 1bc3da6

Please sign in to comment.