Skip to content

Commit

Permalink
refactor Color to its own namespace, add fromJson
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsjo committed Nov 12, 2024
1 parent 38ca03a commit f7314fc
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 31 deletions.
9 changes: 4 additions & 5 deletions src/core/Effects.ts
Original file line number Diff line number Diff line change
@@ -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<string, number>([
Expand Down Expand Up @@ -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)
Expand All @@ -162,15 +161,15 @@ 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]
b += amplifier * rgb[2]
total += amplifier
}
if (total === 0) {
return intToRgb(-13083194)
return Color.intToRgb(-13083194)
}
r = r / total
g = g / total
Expand Down
15 changes: 7 additions & 8 deletions src/render/BlockColors.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 7 additions & 8 deletions src/render/ItemColors.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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([
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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])
})
}

Expand All @@ -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
})
2 changes: 1 addition & 1 deletion src/render/ItemModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/render/VoxelRenderer.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
20 changes: 20 additions & 0 deletions src/util/Color.ts
Original file line number Diff line number Diff line change
@@ -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]
}
}
8 changes: 0 additions & 8 deletions src/util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,3 @@ export function mutateWithDefault<K, V>(map: Map<K, V>, 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]
}
2 changes: 2 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './Color.js'
export * from './Json.js'
export * from './StringReader.js'
export * from './Util.js'

0 comments on commit f7314fc

Please sign in to comment.