MCPcopy Create free account
hub / github.com/ecomfe/tempad-dev / formatHexAlpha

Function formatHexAlpha

packages/extension/utils/css.ts:206–232  ·  view source on GitHub ↗
(
  color: { r: number; g: number; b: number },
  opacity: number = 1
)

Source from the content-addressed store, hash-verified

204}
205
206export function formatHexAlpha(
207 color: { r: number; g: number; b: number },
208 opacity: number = 1
209): string {
210 const toHex = (n: number) => {
211 const i = Math.min(255, Math.max(0, Math.round(n * 255)))
212 return i.toString(16).padStart(2, '0').toUpperCase()
213 }
214
215 const r = toHex(color.r)
216 const g = toHex(color.g)
217 const b = toHex(color.b)
218
219 if (opacity >= 0.99) {
220 if (r[0] === r[1] && g[0] === g[1] && b[0] === b[1]) {
221 return `#${r[0]}${g[0]}${b[0]}`
222 }
223 return `#${r}${g}${b}`
224 }
225
226 const a = toHex(opacity)
227 if (r[0] === r[1] && g[0] === g[1] && b[0] === b[1] && a[0] === a[1]) {
228 return `#${r[0]}${g[0]}${b[0]}${a[0]}`
229 }
230
231 return `#${r}${g}${b}${a}`
232}
233
234export function parseBackgroundShorthand(value: string) {
235 const result: {

Callers 10

resolveSolidPaintFunction · 0.90
formatGradientStopColorFunction · 0.90
resolveBoundSolidPaintFunction · 0.90
css.test.tsFile · 0.90
serializeVariableValueFunction · 0.90
resolveSolidPaintChannelFunction · 0.90
serializeVariableValueFunction · 0.90
resolveSolidPaintColorFunction · 0.90
formatGradientStopColorFunction · 0.90
resolveRunAttrsFunction · 0.90

Calls 1

toHexFunction · 0.70

Tested by

no test coverage detected