LCOV - code coverage report
Current view: top level - packages/layer-editor/src - fill.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 11.3 % 62 7
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { Point2D } from '@comfyorg/shared-frontend-utils/geometry'
       2              : export interface GradientStop {
       3              :   offset: number
       4              :   color: string
       5              :   alpha?: number
       6              : }
       7              : 
       8              : export type FillSpec =
       9              :   | { type: 'solid'; color: string }
      10              :   | { type: 'linear'; angle: number; stops: GradientStop[] }
      11              :   | {
      12              :       type: 'radial'
      13              :       cx: number
      14              :       cy: number
      15              :       radius: number
      16              :       stops: GradientStop[]
      17              :     }
      18              : 
      19            2 : const clamp01 = (v: number): number => Math.max(0, Math.min(1, v))
      20            2 : const num = (v: unknown, d: number): number =>
      21              :   typeof v === 'number' && isFinite(v) ? v : d
      22              : 
      23            0 : export function defaultFillSpec(): FillSpec {
      24            0 :   return { type: 'solid', color: '#808080' }
      25              : }
      26              : 
      27            0 : export function defaultGradientStops(): GradientStop[] {
      28            0 :   return [
      29              :     { offset: 0, color: '#000000' },
      30              :     { offset: 1, color: '#ffffff' }
      31              :   ]
      32              : }
      33              : 
      34            0 : function normalizeStops(raw: unknown): GradientStop[] {
      35            0 :   const arr = Array.isArray(raw) ? raw : []
      36            0 :   const stops: GradientStop[] = []
      37            0 :   for (const s of arr) {
      38            0 :     const r = (s ?? {}) as Record<string, unknown>
      39            0 :     if (typeof r.color !== 'string') continue
      40            0 :     stops.push({
      41              :       offset: clamp01(num(r.offset, 0)),
      42              :       color: r.color,
      43              :       alpha: r.alpha === undefined ? undefined : clamp01(num(r.alpha, 1))
      44              :     })
      45              :   }
      46            0 :   stops.sort((a, b) => a.offset - b.offset)
      47            0 :   return stops.length >= 2 ? stops : defaultGradientStops()
      48              : }
      49              : 
      50            2 : export function normalizeFillSpec(raw: unknown): FillSpec {
      51            2 :   const r = (raw ?? {}) as Record<string, unknown>
      52            2 :   if (r.type === 'linear') {
      53            0 :     return {
      54              :       type: 'linear',
      55              :       angle: num(r.angle, 0) % 360,
      56              :       stops: normalizeStops(r.stops)
      57              :     }
      58              :   }
      59            2 :   if (r.type === 'radial') {
      60            0 :     return {
      61              :       type: 'radial',
      62              :       cx: clamp01(num(r.cx, 0.5)),
      63              :       cy: clamp01(num(r.cy, 0.5)),
      64              :       radius: Math.max(0.01, Math.min(4, num(r.radius, 1))),
      65              :       stops: normalizeStops(r.stops)
      66              :     }
      67              :   }
      68            2 :   return {
      69              :     type: 'solid',
      70              :     color: typeof r.color === 'string' ? r.color : '#808080'
      71              :   }
      72              : }
      73              : 
      74              : export function cloneFillSpec(spec: FillSpec): FillSpec {
      75              :   if (spec.type === 'solid') return { ...spec }
      76              :   if (spec.type === 'linear')
      77              :     return { ...spec, stops: spec.stops.map((s) => ({ ...s })) }
      78              :   return { ...spec, stops: spec.stops.map((s) => ({ ...s })) }
      79              : }
      80              : 
      81            0 : export function fillSpecStamp(spec: FillSpec): string {
      82            0 :   return JSON.stringify(spec)
      83              : }
      84              : 
      85            0 : export function linearEndpoints(
      86              :   angle: number,
      87              :   w: number,
      88              :   h: number
      89              : ): { from: Point2D; to: Point2D } {
      90            0 :   const a = (angle * Math.PI) / 180
      91            0 :   const dx = Math.cos(a)
      92            0 :   const dy = Math.sin(a)
      93            0 :   const half = (Math.abs(w * dx) + Math.abs(h * dy)) / 2
      94            0 :   const cx = w / 2
      95            0 :   const cy = h / 2
      96            0 :   return {
      97              :     from: { x: cx - dx * half, y: cy - dy * half },
      98              :     to: { x: cx + dx * half, y: cy + dy * half }
      99              :   }
     100              : }
     101              : 
     102            0 : function stopColor(stop: GradientStop): string {
     103            0 :   if (stop.alpha === undefined || stop.alpha >= 1) return stop.color
     104            0 :   const hex = stop.color.replace('#', '')
     105            0 :   const full =
     106              :     hex.length === 3
     107              :       ? hex
     108              :           .split('')
     109              :           .map((c) => c + c)
     110              :           .join('')
     111              :       : hex
     112              :   const r = parseInt(full.slice(0, 2), 16)
     113              :   const g = parseInt(full.slice(2, 4), 16)
     114              :   const b = parseInt(full.slice(4, 6), 16)
     115            0 :   return `rgba(${r},${g},${b},${stop.alpha})`
     116              : }
     117              : 
     118            0 : export function paintFillInto(
     119              :   g: CanvasRenderingContext2D,
     120              :   spec: FillSpec,
     121              :   w: number,
     122              :   h: number
     123              : ): void {
     124            0 :   if (spec.type === 'solid') {
     125            0 :     g.fillStyle = spec.color
     126            0 :     g.fillRect(0, 0, w, h)
     127            0 :     return
     128              :   }
     129            0 :   let gradient: CanvasGradient
     130            0 :   if (spec.type === 'linear') {
     131            0 :     const { from, to } = linearEndpoints(spec.angle, w, h)
     132            0 :     gradient = g.createLinearGradient(from.x, from.y, to.x, to.y)
     133              :   } else {
     134            0 :     const cx = spec.cx * w
     135            0 :     const cy = spec.cy * h
     136            0 :     const r = Math.max(1e-3, spec.radius * (Math.hypot(w, h) / 2))
     137            0 :     gradient = g.createRadialGradient(cx, cy, 0, cx, cy, r)
     138              :   }
     139            0 :   for (const stop of spec.stops)
     140            0 :     gradient.addColorStop(stop.offset, stopColor(stop))
     141            0 :   g.fillStyle = gradient
     142            0 :   g.fillRect(0, 0, w, h)
     143              : }
     144              : 
     145            0 : export function renderFillBitmap(
     146              :   spec: FillSpec,
     147              :   w: number,
     148              :   h: number
     149              : ): HTMLCanvasElement | null {
     150            0 :   const canvas = document.createElement('canvas')
     151            0 :   canvas.width = Math.max(1, w)
     152            0 :   canvas.height = Math.max(1, h)
     153            0 :   const g = canvas.getContext('2d')
     154            0 :   if (!g) return null
     155            0 :   paintFillInto(g, spec, canvas.width, canvas.height)
     156            0 :   return canvas
     157              : }
        

Generated by: LCOV version 2.0-1