WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / CSS

rgb() — direkte Rot-, Grün- und Blaukanäle

`rgb()` und der Kompatibilitätsalias `rgba()` bestimmen eine Farbe in `sRGB` aus drei Kanälen und optionalem Alpha.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

rgb( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) | rgb( <legacy-rgb-syntax> )

rgb() und der Kompatibilitätsalias rgba() bestimmen eine Farbe in sRGB aus drei Kanälen und optionalem Alpha.

rgb(300 -10 128 / 50%) wird zu rgba(255, 0, 128, 0.5); der transparente Canvas-Pixel ist 255,0,128,128. Moderne Einheitenmischung rgb(100% 0 128 / 50%) ergibt true, die Kommaform rgb(100%, 0, 0%) false, und rgb(none 0 0) mit fehlender Komponente wird mit true unterstützt.

Grammatik: rgb( [ <number> | <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) | rgb( <legacy-rgb-syntax> ).

Modernes rgb(100% 0 128 / 50%) trennt Kanäle durch Leerraum und Alpha durch /; altes rgb(255, 0, 128) nutzt Kommas. Modern dürfen Zahlen und Prozente gemischt werden und none ist zulässig; alt müssen alle Kanäle denselben Typ haben und none ist verboten. 255 und 100% sind das Kanalmaximum, fehlendes Alpha ist deckend, Werte außerhalb des Bereichs werden beim Parsen begrenzt.

Beispiele

const value='rgb(300 -10 128 / 50%)';
const probe=document.createElement('div');probe.style.backgroundColor=value;document.body.append(probe);
const canvas=document.createElement('canvas');canvas.width=canvas.height=1;const context=canvas.getContext('2d');context.clearRect(0,0,1,1);context.fillStyle=value;context.fillRect(0,0,1,1);
const pixel=[...context.getImageData(0,0,1,1).data].join(',');
const modernMixed=CSS.supports('color','rgb(100% 0 128 / 50%)');
const legacyMixed=CSS.supports('color','rgb(100%, 0, 0%)');
const missing=CSS.supports('color','rgb(none 0 0)');
console.log('computed='+getComputedStyle(probe).backgroundColor+'; pixel='+pixel+'; modern-mixed='+modernMixed+'; legacy-mixed='+legacyMixed+'; none='+missing);

Ergebnis

computed=rgba(255, 0, 128, 0.5); pixel=255,0,128,128; modern-mixed=true; legacy-mixed=false; none=true

`rgb()` und der Kompatibilitätsalias `rgba()` bestimmen eine Farbe in `sRGB` aus drei Kanälen und optionalem Alpha.

Quellen