KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / CSS

alpha() — relatieve wijziging van het alfakanaal

`alpha()` maakt een relatieve kleur die de kanalen van de bronkleur behoudt en alleen het alfakanaal vervangt of berekent.

Versie: 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() maakt een relatieve kleur die de kanalen van de bronkleur behoudt en alleen het alfakanaal vervangt of berekent.

Het voorbeeld gebruikt alpha(from rgb(20 40 60 / 80%) / calc(alpha * .5)): de kleurkanalen blijven gelijk, alfa daalt van 0.8 naar 0.4 en compositie op wit levert een meetbare pixel op.

Grammatica: alpha(from <color> / [<alpha-value> | none]).

Het componentwoord alpha geeft het bronalfa als getal, waarbij 1 volledig dekkend betekent. Het resultaat blijft in de kleurruimte van de bron, berekend alfa wordt begrensd tot 0–1 en none behoudt een ontbrekend component. De functie alpha() is in CSS Color 5 momenteel als „at-risk” gemarkeerd en kan dus worden verwijderd voordat dat specificatieniveau stabiel wordt.

Voorbeelden

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(','));

Resultaat

computed=color(srgb 0.0784314 0.156863 0.235294 / 0.4); over-white=161,169,177

`alpha()` maakt een relatieve kleur die de kanalen van de bronkleur behoudt en alleen het alfakanaal vervangt of berekent.

Bronnen