mirror of
https://github.com/trafficlunar/blockmatic.git
synced 2026-06-28 06:34:13 +00:00
fix: findBlockFromRgb()
This commit is contained in:
parent
abd869bf0a
commit
6ddcd1bbdf
3 changed files with 17 additions and 21 deletions
14
src/utils/findBlockFromRgb.ts
Normal file
14
src/utils/findBlockFromRgb.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import _blockData from "@/data/blocks/programmer-art/data.json";
|
||||
const blockData: BlockData = _blockData;
|
||||
|
||||
export const findBlockFromRgb = (r: number, g: number, b: number, a: number): string => {
|
||||
return Object.entries(blockData).reduce(
|
||||
(closestBlock, [block, data]) => {
|
||||
const distance = Math.sqrt(
|
||||
Math.pow(r - data.color[0], 2) + Math.pow(g - data.color[1], 2) + Math.pow(b - data.color[2], 2) + Math.pow(a - data.color[3], 2)
|
||||
);
|
||||
return distance < closestBlock.distance ? { block, distance } : closestBlock;
|
||||
},
|
||||
{ block: "", distance: Infinity }
|
||||
).block;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue