@@ -267,3 +279,33 @@ export const ImageDetailSection = defineComponent({
)
},
})
+
+const BlurHashPreview = defineComponent({
+ props: {
+ hash: {
+ type: String,
+ required: true,
+ },
+ },
+ setup(props) {
+ const canvasRef = ref(null)
+
+ onMounted(() => {
+ const canvas = canvasRef.value!
+ const ctx = canvas.getContext('2d')!
+ const pixels = decode(props.hash, 32, 32)
+ const imageData = ctx.createImageData(32, 32)
+ imageData.data.set(pixels)
+ ctx.putImageData(imageData, 0, 0)
+ })
+
+ return () => (
+
+ )
+ },
+})
diff --git a/src/models/base.ts b/src/models/base.ts
index e8ae0196f..961c1a4c2 100644
--- a/src/models/base.ts
+++ b/src/models/base.ts
@@ -12,6 +12,7 @@ export interface Image {
type: string
accent?: string
src: string
+ blurHash?: string
}
export class BaseModel {
diff --git a/src/utils/image.ts b/src/utils/image.ts
index 474890801..316ada0aa 100644
--- a/src/utils/image.ts
+++ b/src/utils/image.ts
@@ -1,9 +1,10 @@
// @see https://stackoverflow.com/questions/2541481/get-average-color-of-image-via-javascript
-// @ts-nocheck
+
+import { encode } from 'blurhash'
export function getDominantColor(imageObject: HTMLImageElement) {
const canvas = document.createElement('canvas'),
- ctx = canvas.getContext('2d')
+ ctx = canvas.getContext('2d')!
canvas.width = 1
canvas.height = 1
@@ -26,3 +27,20 @@ export function rgbToHex(red: number, green: number, blue: number) {
export function rgbObjectToHex(rgb: { r: number; g: number; b: number }) {
return rgbToHex(rgb.r, rgb.g, rgb.b)
}
+
+export function getBlurHash(imageObject: HTMLImageElement) {
+ const canvas = document.createElement('canvas'),
+ ctx = canvas.getContext('2d')!
+
+ canvas.width = imageObject.naturalWidth
+ canvas.height = imageObject.naturalHeight
+
+ ctx.drawImage(imageObject, 0, 0)
+
+ const imageData = ctx.getImageData(0, 0, 32, 32)
+ const pixels = new Uint8ClampedArray(imageData.data)
+ const componentX = 4
+ const componentY = 4
+
+ return encode(pixels, 32, 32, componentX, componentY)
+}