CKTIS / CSS
alpha() — relative Änderung des Alphakanals
`alpha()` erzeugt eine relative Farbe, die die Kanäle der Ursprungsfarbe beibehält und nur ihren Alphakanal ersetzt oder berechnet.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
alpha(from <color> / [<alpha-value> | none])alpha() erzeugt eine relative Farbe, die die Kanäle der Ursprungsfarbe beibehält und nur ihren Alphakanal ersetzt oder berechnet.
Das Beispiel verwendet alpha(from rgb(20 40 60 / 80%) / calc(alpha * .5)): Die Farbkanäle bleiben gleich, Alpha sinkt von 0.8 auf 0.4, und die Komposition auf Weiß ergibt ein messbares Pixel.
Grammatik: alpha(from <color> / [<alpha-value> | none]).
Das Komponentenwort alpha stellt das Ursprungsalpha als Zahl bereit, wobei 1 volle Deckkraft bedeutet. Das Ergebnis bleibt im Farbraum des Ursprungs, berechnetes Alpha wird auf den Bereich von 0 bis 1 begrenzt, und none bewahrt eine fehlende Komponente. Die Funktion alpha() ist in CSS Color 5 derzeit als „at-risk“ gekennzeichnet und kann daher vor der Stabilisierung dieser Spezifikationsstufe entfernt werden.
Beispiele
const swatch=document.createElement('div');document.body.append(swatch);swatch.style.backgroundColor='alpha(from rgb(20 40 60 / 80%) / calc(alpha * .5))';const computed=getComputedStyle(swatch).backgroundColor;if(!computed.includes('/ 0.4'))throw new Error('alpha() did not preserve the expected half alpha: '+computed);const canvas=document.createElement('canvas');canvas.width=1;canvas.height=1;const ctx=canvas.getContext('2d');ctx.fillStyle='white';ctx.fillRect(0,0,1,1);ctx.fillStyle=computed;ctx.fillRect(0,0,1,1);const pixel=[...ctx.getImageData(0,0,1,1).data];if(pixel.slice(0,3).join(',')!=='161,169,177')throw new Error('unexpected alpha composite: '+pixel);console.log('computed='+computed+'; over-white='+pixel.slice(0,3).join(','));Ergebnis
computed=color(srgb 0.0784314 0.156863 0.235294 / 0.4); over-white=161,169,177
`alpha()` erzeugt eine relative Farbe, die die Kanäle der Ursprungsfarbe beibehält und nur ihren Alphakanal ersetzt oder berechnet.