Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
import type { FaceSkinPalette } from 'face-skin'
import { DEFAULT_FACE_PRIMARY_COLOR, type FaceState, toPiuColorNumber, toPiuColorString } from 'face-state'
import { type Skin as PiuSkin, Skin } from 'piu/MC'
import type { Shape as PiuShape } from 'piu/shape'
export const FULL_TURN = 2 * Math.PI
export {
quantizeUnit,
rememberCachedValue,
SHAPE_CACHE_ENTRY_LIMIT,
UNIT_OPEN_STEPS,
unitFromStep,
} from 'parts/shape-cache'
let fillSkinCache: Map<number, PiuSkin> | null = null
let strokeSkinCache: Map<number, PiuSkin> | null = null
let fillStrokeSkinCache: Map<number, PiuSkin> | null = null
export class FacePrimaryColorBehavior extends Behavior {
#hasPalette = false
#color = DEFAULT_FACE_PRIMARY_COLOR
onFaceSkin(shape: PiuShape, palette: FaceSkinPalette) {
this.#hasPalette = true
shape.skin = palette.primary
}
onFaceState(shape: PiuShape, face: FaceState) {
if (this.#hasPalette) return
const color = toPiuColorNumber(face.theme.primary)
if (color === this.#color) return
this.#color = color
shape.skin = getFillSkin(color)
}
}
export function getFillSkin(color: number): PiuSkin {
if (!fillSkinCache) fillSkinCache = new Map()
let skin = fillSkinCache.get(color)
if (skin) return skin
skin = new Skin({ fill: toPiuColorString(color) })
fillSkinCache.set(color, skin)
return skin
}
export function getStrokeSkin(color: number): PiuSkin {
if (!strokeSkinCache) strokeSkinCache = new Map()
let skin = strokeSkinCache.get(color)
if (skin) return skin
skin = new Skin({ stroke: toPiuColorString(color) })
strokeSkinCache.set(color, skin)
return skin
}
export function getFillStrokeSkin(color: number): PiuSkin {
if (!fillStrokeSkinCache) fillStrokeSkinCache = new Map()
let skin = fillStrokeSkinCache.get(color)
if (skin) return skin
const value = toPiuColorString(color)
skin = new Skin({ fill: value, stroke: value })
fillStrokeSkinCache.set(color, skin)
return skin
}
54d761463This file is pinned to the selected release. Download the original to open it in your design tool.