UNPKG

@cornerstonejs/core

Version:
88 lines (87 loc) 2.85 kB
export default class CanvasProperties { constructor(actor) { this.opacity = 0.4; this.outlineOpacity = 0.4; this.transferFunction = []; this.colorCache = new Map(); this.actor = actor; } setRGBTransferFunction(index, cfun) { this.transferFunction[index] = cfun; this.invalidateColorCache(); } getRGBTransferFunction(index = 0) { return this.transferFunction[index]; } setScalarOpacity(indexOrOpacity, scalarOpacityFunction) { if (scalarOpacityFunction?.getValue) { this.scalarOpacityFunction = scalarOpacityFunction; this.invalidateColorCache(); return; } if (indexOrOpacity?.getValue) { this.scalarOpacityFunction = indexOrOpacity; this.invalidateColorCache(); return; } if (typeof indexOrOpacity === 'number') { this.opacity = indexOrOpacity; this.invalidateColorCache(); } } getScalarOpacity() { return this.scalarOpacityFunction; } getOpacity() { return this.opacity; } setInterpolationTypeToNearest() { } setUseLabelOutline() { } setLabelOutlineOpacity(opacity) { this.outlineOpacity = opacity; } setLabelOutlineThickness() { } modified() { this.invalidateColorCache(); } getColorBytes(index, destination, offset = 0) { let color = this.colorCache.get(index); if (!color) { const cfun = this.transferFunction[0]; if (!cfun) { color = [0, 0, 0, 0]; } else { const rawOpacity = this.scalarOpacityFunction?.getValue ? this.scalarOpacityFunction.getValue(index) : this.opacity; const opacity = Math.min(Math.max(rawOpacity, 0), 1); color = [ Math.round(Math.min(Math.max(cfun.getRedValue(index), 0), 1) * 255), Math.round(Math.min(Math.max(cfun.getGreenValue(index), 0), 1) * 255), Math.round(Math.min(Math.max(cfun.getBlueValue(index), 0), 1) * 255), Math.round(opacity * 255), ]; } this.colorCache.set(index, color); } if (destination) { destination[offset] = color[0]; destination[offset + 1] = color[1]; destination[offset + 2] = color[2]; destination[offset + 3] = color[3]; } return color; } getColor(index) { const [r, g, b, opacity] = this.getColorBytes(index); return [r / 255, g / 255, b / 255, opacity / 255]; } invalidateColorCache() { this.colorCache.clear(); this.actor.modified(); } }