From f7314fcac64f0533af2af6a5a605ff75b80ab826 Mon Sep 17 00:00:00 2001 From: Jochen Jacobs Date: Tue, 12 Nov 2024 15:37:08 +0100 Subject: [PATCH] refactor Color to its own namespace, add fromJson --- src/core/Effects.ts | 9 ++++----- src/render/BlockColors.ts | 15 +++++++-------- src/render/ItemColors.ts | 15 +++++++-------- src/render/ItemModel.ts | 2 +- src/render/VoxelRenderer.ts | 2 +- src/util/Color.ts | 20 ++++++++++++++++++++ src/util/Util.ts | 8 -------- src/util/index.ts | 2 ++ 8 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 src/util/Color.ts diff --git a/src/core/Effects.ts b/src/core/Effects.ts index 7e968c2..9445804 100644 --- a/src/core/Effects.ts +++ b/src/core/Effects.ts @@ -1,7 +1,6 @@ import type { NbtCompound, NbtTag } from '../nbt/index.js' import { NbtType } from '../nbt/index.js' -import type { Color } from '../util/index.js' -import { intToRgb } from '../util/index.js' +import { Color } from '../util/index.js' import { Identifier } from './Identifier.js' export const EFFECT_COLORS = new Map([ @@ -139,7 +138,7 @@ export namespace PotionContents { export function getColor(contents: PotionContents): Color { if (contents.customColor) { - return intToRgb(contents.customColor) + return Color.intToRgb(contents.customColor) } const effects = getAllEffects(contents) return mixEffectColors(effects) @@ -162,7 +161,7 @@ export namespace PotionContents { for (const effect of effects) { const color = EFFECT_COLORS.get(effect.effect.toString()) if (color === undefined) continue - const rgb = intToRgb(color) + const rgb = Color.intToRgb(color) const amplifier = effect.amplifier + 1 r += amplifier * rgb[0] g += amplifier * rgb[1] @@ -170,7 +169,7 @@ export namespace PotionContents { total += amplifier } if (total === 0) { - return intToRgb(-13083194) + return Color.intToRgb(-13083194) } r = r / total g = g / total diff --git a/src/render/BlockColors.ts b/src/render/BlockColors.ts index 7787dd6..2290cd3 100644 --- a/src/render/BlockColors.ts +++ b/src/render/BlockColors.ts @@ -1,14 +1,13 @@ import { clamp } from '../math/index.js' -import type { Color } from '../util/index.js' -import { intToRgb } from '../util/index.js' +import { Color } from '../util/index.js' const grass: Color = [124 / 255, 189 / 255, 107 / 255] -const spruce = intToRgb(6396257) -const birch = intToRgb(8431445) -const foliage = intToRgb(4764952) -const water = intToRgb(4159204) -const attached_stem = intToRgb(8431445) -const lily_pad = intToRgb(2129968) +const spruce = Color.intToRgb(6396257) +const birch = Color.intToRgb(8431445) +const foliage = Color.intToRgb(4764952) +const water = Color.intToRgb(4159204) +const attached_stem = Color.intToRgb(8431445) +const lily_pad = Color.intToRgb(2129968) const redstone = (power: number): Color => { const a = power / 15 diff --git a/src/render/ItemColors.ts b/src/render/ItemColors.ts index fee65e7..7f00081 100644 --- a/src/render/ItemColors.ts +++ b/src/render/ItemColors.ts @@ -1,8 +1,7 @@ import type { ItemStack } from '../core/index.js' import { Identifier, PotionContents } from '../core/index.js' import { NbtIntArray } from '../index.js' -import type { Color } from '../util/index.js' -import { intToRgb } from '../util/index.js' +import { Color } from '../util/index.js' import { BlockColors } from './BlockColors.js' type Tint = Color | ((index: number) => Color) @@ -24,7 +23,7 @@ function getDyedColor(item: ItemStack, fallback: number) { const dyedColor = item.getComponent('dyed_color', tag => { return tag.isCompound() ? tag.getNumber('rgb') : tag.getAsNumber() }) - return intToRgb(dyedColor ?? fallback) + return Color.intToRgb(dyedColor ?? fallback) } register([ @@ -58,10 +57,10 @@ register([ }) const color: Color = (() => { if (!colors || colors.length === 0) { - return intToRgb(9079434) + return Color.intToRgb(9079434) } if (colors.length === 1) { - return intToRgb(colors.get(0)!.getAsNumber()) + return Color.intToRgb(colors.get(0)!.getAsNumber()) } let [r, g, b] = [0, 0, 0] for (const color of colors.getItems()) { @@ -176,7 +175,7 @@ const SpawnEggs: [string, number, number][] = [ for (const egg of SpawnEggs) { register([`${egg[0]}_spawn_egg`], () => { - return (index: number) => intToRgb(index === 0 ? egg[1] : egg[2]) + return (index: number) => Color.intToRgb(index === 0 ? egg[1] : egg[2]) }) } @@ -199,12 +198,12 @@ for (const id of [ register([ 'mangrove_leaves', -], () => intToRgb(9619016)) +], () => Color.intToRgb(9619016)) register([ 'filled_map', ], item => { const mapColor = item.getComponent('map_color', tag => tag.getAsNumber()) - const color = intToRgb(mapColor ?? 4603950) + const color = Color.intToRgb(mapColor ?? 4603950) return (index: number) => index === 0 ? [1, 1, 1] : color }) diff --git a/src/render/ItemModel.ts b/src/render/ItemModel.ts index c38dc9b..e37d0ec 100644 --- a/src/render/ItemModel.ts +++ b/src/render/ItemModel.ts @@ -69,7 +69,7 @@ export namespace ItemModel { throw new Error(`Model ${this.modelId} does not exist (trying to render ${item.toString()})`) } let tint = undefined // TODO model tints - const mesh = model.getMesh(resources, Cull.none()) + const mesh = model.getMesh(resources, Cull.none(), tint) mesh.transform(model.getDisplayTransform(context.display_context ?? 'gui')) return mesh } diff --git a/src/render/VoxelRenderer.ts b/src/render/VoxelRenderer.ts index f85b800..ed3fe7f 100644 --- a/src/render/VoxelRenderer.ts +++ b/src/render/VoxelRenderer.ts @@ -1,6 +1,6 @@ import type { mat4 } from 'gl-matrix' import { Vector } from '../math/index.js' -import type { Color } from '../util/index.js' +import type { Color } from '../util/Color.js' import { mutateWithDefault } from '../util/index.js' import { Mesh } from './Mesh.js' import { Quad } from './Quad.js' diff --git a/src/util/Color.ts b/src/util/Color.ts new file mode 100644 index 0000000..045691c --- /dev/null +++ b/src/util/Color.ts @@ -0,0 +1,20 @@ +import { Json } from "./Json.js" + +export type Color = [number, number, number] + +export namespace Color { + export function fromJson(obj: unknown): Color | undefined { + const packed = Json.readNumber(obj) + if (packed) return intToRgb(packed) + const array = Json.readArray(obj, o => Json.readNumber(o) ?? 0) + if (array === undefined || array.length !== 3) return undefined + return array as [number, number, number] + } + + export function intToRgb(n: number): Color { + const r = (n >> 16) & 255 + const g = (n >> 8) & 255 + const b = n & 255 + return [r / 255, g / 255, b / 255] + } +} \ No newline at end of file diff --git a/src/util/Util.ts b/src/util/Util.ts index cb6cb05..940a785 100644 --- a/src/util/Util.ts +++ b/src/util/Util.ts @@ -26,11 +26,3 @@ export function mutateWithDefault(map: Map, key: K, initialValue: V, return value } -export type Color = [number, number, number] - -export function intToRgb(n: number): Color { - const r = (n >> 16) & 255 - const g = (n >> 8) & 255 - const b = n & 255 - return [r / 255, g / 255, b / 255] -} diff --git a/src/util/index.ts b/src/util/index.ts index b2495f9..ca3c298 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -1,3 +1,5 @@ +export * from './Color.js' export * from './Json.js' export * from './StringReader.js' export * from './Util.js' +