Sass color.space()

Beginner
10 min read
Updated: Nov 2026
5 examples
Dart Sass 1.79+

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

SCSS
@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.

space vs is-legacy

Two related probes:

HelperReturnsUse when
color.space($c)Unquoted space nameYou need the exact space (oklch, xyz, …)
color.is-legacy($c)true / falseYou only care if it is rgb / hsl / hwb
Hex / named colorsspace → rgbAlso is-legacy → true
xyz-d65 writingsspace → xyzxyz-d65 is an alias of xyz
Compare unquoted names.

Write if(color.space($c) == oklch, …) — no quotes around the space name in the comparison.

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.

Example 1 — Hex Reports rgb (Official Docs)

Legacy hex lives in the rgb space.

SCSS
@use "sass:color";

@debug color.space(#036); // rgb

.swatch {
  --space: #{color.space(#036)};
}

How It Works

  1. Hex navy is a legacy RGB color.
  2. color.space returns the unquoted string rgb.
  3. Interpolation writes that name into a custom property.
  4. Named colors like navy report rgb too.

Example 2 — HSL Literal Reports hsl (Official Docs)

Written space stays the space name.

SCSS
@use "sass:color";

@debug color.space(hsl(120deg 40% 50%)); // hsl

.leaf {
  --space: #{color.space(hsl(120deg 40% 50%))};
}

How It Works

  1. HSL constructor creates a color in the hsl space.
  2. Reader reports that space name.
  3. Legacy too — is-legacy would be true.
  4. 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.

SCSS
@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

  1. CSS color() writing uses the xyz-d65 label.
  2. Sass normalizes the space name to xyz.
  3. Comparisons should check == xyz, not xyz-d65.
  4. Matches the official Sass docs sample.

Example 4 — Modern Spaces Keep Their Names

oklch and friends report themselves.

SCSS
@use "sass:color";

.modern {
  --oklch: #{color.space(oklch(70% 10% 120deg))};
  --lab: #{color.space(lab(50% -20 30))};
}

How It Works

  1. Modern constructors keep their space identity.
  2. is-legacy would be false for both.
  3. space tells you which modern space.
  4. Handy before choosing $space: on scale/adjust.

Example 5 — Branch Before Converting

Only convert when the token is not already in the target space.

SCSS
@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")};
}

How It Works

  1. Hex brand is still rgb → needs convert.
  2. Accent is already oklch → ready.
  3. Unquoted compare uses == oklch.
  4. 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+.

Sass Support

color.space() is a modern module helper. Prefer Dart Sass 1.79+ with @use "sass:color".

APIStatus
color.space()Dart Sass 1.79+
Global space()Not provided
LibSass / Ruby SassUnsupported
BrowsersN/A — only see compiled CSS values

Compile with the Dart Sass CLI.

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.

Compile Notes

Practical checklist:

checklist.txt
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

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.
color.space() needs Dart Sass 1.79+. Older compilers report Undefined function.
Hex colors live in the legacy rgb space, so color.space(#036) returns rgb. Named colors like navy also report rgb.
space() returns the space name string (rgb, oklch, xyz, …). is-legacy() returns a boolean that is true only for rgb, hsl, and hwb.
Yes. Compare against unquoted names such as if(color.space($c) == rgb, …, …) or == oklch.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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