MCPcopy Index your code
hub / github.com/anomalyco/opencode / generateGrayScale

Function generateGrayScale

packages/opencode/src/cli/cmd/run/theme.ts:328–367  ·  view source on GitHub ↗
(bg: RGBA, isDark: boolean, map: (rgba: RGBA) => RGBA)

Source from the content-addressed store, hash-verified

326}
327
328function generateGrayScale(bg: RGBA, isDark: boolean, map: (rgba: RGBA) => RGBA): Record<number, RGBA> {
329 const r = bg.r * 255
330 const g = bg.g * 255
331 const b = bg.b * 255
332 const lum = 0.299 * r + 0.587 * g + 0.114 * b
333 const cast = 0.25 * (1 - chroma(bg)) ** 2
334
335 const gray = (level: number) => {
336 const factor = level / 12
337
338 if (isDark && lum < 10) {
339 const value = Math.floor(factor * 0.4 * 255)
340 return map(RGBA.fromInts(value, value, value))
341 }
342
343 if (!isDark && lum > 245) {
344 const value = Math.floor(255 - factor * 0.4 * 255)
345 return map(RGBA.fromInts(value, value, value))
346 }
347
348 const value = isDark ? lum + (255 - lum) * factor * 0.4 : lum * (1 - factor * 0.4)
349 const tone = RGBA.fromInts(Math.floor(value), Math.floor(value), Math.floor(value))
350 if (cast === 0) return map(tone)
351
352 const ratio = lum === 0 ? 0 : value / lum
353 return map(
354 tint(
355 tone,
356 RGBA.fromInts(
357 Math.floor(Math.max(0, Math.min(r * ratio, 255))),
358 Math.floor(Math.max(0, Math.min(g * ratio, 255))),
359 Math.floor(Math.max(0, Math.min(b * ratio, 255))),
360 ),
361 cast,
362 ),
363 )
364 }
365
366 return Object.fromEntries(Array.from({ length: 12 }, (_, index) => [index + 1, gray(index + 1)]))
367}
368
369function generateMutedTextColor(bg: RGBA, isDark: boolean, map: (rgba: RGBA) => RGBA): RGBA {
370 const lum = 0.299 * bg.r * 255 + 0.587 * bg.g * 255 + 0.114 * bg.b * 255

Callers 1

generateSystemFunction · 0.70

Calls 3

chromaFunction · 0.85
grayFunction · 0.85
fromMethod · 0.45

Tested by

no test coverage detected