Home Sass Color color.space() Sass color.space() Definition
Definition and Usage color.space($color) returns the name of $color’s color space as an unquoted string — for example rgb, hsl, oklch, or xyz. Load it with @use "sass:color" on Dart Sass 1.79+ . There is no separate global alias.
Hex codes and CSS named colors report as rgb. Use this helper to audit tokens, branch mixins, or decide when to call color.to-space() .
Official samples. color.space(#036) → rgb. color.space(hsl(120deg 40% 50%)) → hsl. color.space(color(xyz-d65 0.1 0.2 0.3)) → xyz.
Version gate. Needs Dart Sass 1.79+ . Older compilers report Undefined function.
Syntax
Syntax @use "sass:color";
// Dart Sass 1.79+
color.space(#036); // rgb
color.space(hsl(120deg 40% 50%)); // hsl
color.space(color(xyz-d65 0.1 0.2 0.3)); // xyz
// Module only — no separate global space() Pattern: inspect a token → compare the unquoted name in if() → convert with to-space when needed.
Code
SCSS Examples Examples target Dart Sass 1.79+ . Space names are interpolated into custom properties so you can open View Compiled CSS . @debug lines print to the compiler terminal / log.
1. Hex 2. HSL 3. XYZ 4. Modern 5. Guard Example 1 — Hex Reports rgb (Official Docs) Legacy hex lives in the rgb space.
@use "sass:color";
@debug color.space(#036); // rgb
.swatch {
--space: #{color.space(#036)};
} .swatch {
--space: rgb;
} How It Works Hex navy is a legacy RGB color.color.space returns the unquoted string rgb.Interpolation writes that name into a custom property.Named colors like navy report rgb too.Example 2 — HSL Literal Reports hsl (Official Docs) Written space stays the space name.
@use "sass:color";
@debug color.space(hsl(120deg 40% 50%)); // hsl
.leaf {
--space: #{color.space(hsl(120deg 40% 50%))};
} How It Works HSL constructor creates a color in the hsl space.Reader reports that space name.Legacy too — is-legacy would be true.Useful when mixins need the exact space, not only legacy/non-legacy.Example 3 — xyz-d65 Reports xyz (Official Docs) xyz-d65 is an alias; the space name is xyz.
@use "sass:color";
@debug color.space(color(xyz-d65 0.1 0.2 0.3)); // xyz
.cie {
--space: #{color.space(color(xyz-d65 0.1 0.2 0.3))};
} How It Works CSS color() writing uses the xyz-d65 label.Sass normalizes the space name to xyz.Comparisons should check == xyz, not xyz-d65.Matches the official Sass docs sample.Example 4 — Modern Spaces Keep Their Names oklch and friends report themselves.
@use "sass:color";
.modern {
--oklch: #{color.space(oklch(70% 10% 120deg))};
--lab: #{color.space(lab(50% -20 30))};
} .modern {
--oklch: oklch;
--lab: lab;
} How It Works Modern constructors keep their space identity.is-legacy would be false for both.space tells you which modern space.Handy before choosing $space: on scale/adjust.Example 5 — Branch Before Converting Only convert when the token is not already in the target space.
@use "sass:color";
$brand: #036;
$accent: oklch(70% 0.12 250deg);
.report {
--brand: #{color.space($brand)};
--brand-oklch: #{if(color.space($brand) == oklch, "ready", "convert")};
--accent: #{if(color.space($accent) == oklch, "ready", "convert")};
} .report {
--brand: rgb;
--brand-oklch: convert;
--accent: ready;
} How It Works Hex brand is still rgb → needs convert.Accent is already oklch → ready.Unquoted compare uses == oklch.Next step for convert cases: color.to-space($brand, oklch). Edge Cases & Pitfalls Most space() bugs are quoting the name, confusing rgb with srgb, or using old compilers.
compare Compare unquoted names Write == rgb / == oklch. Do not compare against quoted "rgb" unless you intentionally convert types.
alias xyz-d65 reports as xyz Official sample: color(xyz-d65 …) → xyz. Check for xyz in branches.
srgb Legacy rgb ≠ modern srgb color(srgb …) reports srgb, not rgb. Pair with is-legacy when auditing.
module No global space() Always @use "sass:color" and call color.space on Dart Sass 1.79+.
Toolchain
Sass Support color.space() is a modern module helper. Prefer Dart Sass 1.79+ with @use "sass:color".
API Status color.space()Dart Sass 1.79+ Global space() Not provided LibSass / Ruby Sass Unsupported Browsers N/A — only see compiled CSS values
Compile with the Dart Sass CLI .
Remember
Key Takeaways Reads the space name as an unquoted string.
Hex / names report rgb.
Pair with is-legacy when you only need a boolean legacy check.
Module only on Dart Sass 1.79+ — no global space().
One line: use color.space to learn where a token lives before you convert or scale it.
Practice
Compile Notes Practical checklist:
1. Require Dart Sass 1.79+
2. @use "sass:color"
3. Read color.space($c) as an unquoted name
4. Compare with == rgb / == oklch (unquoted)
5. Convert with color.to-space() when the space is wrong Frequently Asked Questions What does Sass color.space() do? color.space($color) returns the name of that color’s space as an unquoted string. Example: color.space(#036) is rgb; color.space(hsl(120deg 40% 50%)) is hsl; color.space(color(xyz-d65 0.1 0.2 0.3)) is xyz.
Which Dart Sass version do I need? color.space() needs Dart Sass 1.79+. Older compilers report Undefined function.
What does a hex color report? Hex colors live in the legacy rgb space, so color.space(#036) returns rgb. Named colors like navy also report rgb.
How is color.space() different from color.is-legacy()? space() returns the space name string (rgb, oklch, xyz, …). is-legacy() returns a boolean that is true only for rgb, hsl, and hwb.
Can I compare the result in if()? Yes. Compare against unquoted names such as if(color.space($c) == rgb, …, …) or == oklch.
Is there a global space()? No. Use the module form color.space() after @use "sass:color".
Next: Sass color.to-gamut() Map a color into a target gamut with an explicit method.
color.to-gamut() → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion