color.space() belongs to the built-in sass:color module. It answers: which color space is this color in? This page covers the unquoted string return value, hex / hsl / xyz / oklch examples, how it pairs with is-legacy, the Dart Sass 1.79+ requirement, and five examples.
01
Concept
Space name reader
02
Module
@use "sass:color"
03
Returns
Unquoted string
04
Legacy
rgb · hsl · hwb
05
Vs
is-legacy()
06
Practice
5 examples
Concept
What Is color.space()?
Space means “tell me the coordinate system this color lives in.” Official docs: returns the name of $color’s space as an unquoted string. A hex brand reports rgb; an HSL literal reports hsl; an XYZ color reports xyz.
color.space(#036) → rgb
color.space(hsl(120deg 40% 50%)) → hsl
color.space(color(xyz-d65 0.1 0.2 0.3)) → xyz
💡
Beginner tip
Writing a color as hex does not create a special “hex space.” Hex is just a notation for rgb—so color.space(#036) is rgb.
There is no separate global space() built-in. Use the module form on Dart Sass 1.79+.
styles.scss
// Recommended — namespaced (Dart Sass 1.79+)
@use "sass:color";
$name: color.space(#036);
// Optional — bring members into the current namespace
@use "sass:color" as *;
$name: space(#036);
// Optional — custom namespace
@use "sass:color" as c;
$name: c.space(#036);
⚠️
Version gate
If Dart Sass reports Undefined function for color.space, upgrade to 1.79+.
Names
🎨 Common Space Names You Will See
Official samples cover rgb, hsl, and xyz. In real projects you will also meet modern names like oklch and srgb.
How you write the color
Typical color.space()
Hex / rgb() / named color
rgb
hsl(…) / hsla(…)
hsl
hwb(…)
hwb
oklch(…)
oklch
color(srgb …)
srgb (modern—not legacy rgb)
color(xyz-d65 …)
xyz
Compare
📋 space vs is-legacy
Both inspect a color’s space, but they answer different questions.
Helper
Returns
Typical use
color.space()
Space name string
“Is this oklch or rgb?”
color.is-legacy()
Boolean
“Is this rgb/hsl/hwb?”
💡
Pair them
Use space when you need the exact name for branching or debugging. Use is-legacy when you only care about the legacy trio for adjust / change behavior.
Support
🛠 Compatibility
This is about Sass compilers, not browsers. The space name is resolved at compile time—CSS never sees a color.space() call (unless you interpolate the string into a custom property).
Feature
Dart Sass
Notes
color.space()
1.79+
Part of the modern color-spaces work
Related color.is-legacy()
1.79+
Boolean for rgb/hsl/hwb
Related color.to-space()
1.79+
Convert a color into another space
LibSass / Ruby Sass
✗
No modern module color-space API
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import color
@use "sass:color";
Hex space
color.space(#036) → rgb
HSL space
color.space(hsl(120deg 40% 50%)) → hsl
XYZ space
color.space(color(xyz-d65 0.1 0.2 0.3)) → xyz
Branch in SCSS
if(color.space($c) == oklch, …, …)
Legacy check
color.is-legacy($c)
Hands-On
Examples Gallery
Examples target Dart Sass 1.79+. Open View Compiled CSS to see the space names Sass emits (official docs results where noted).
📚 Getting Started
Official docs samples for rgb, hsl, and xyz.
Example 1 — Hex, HSL, and XYZ Space Names
Three notations, three space names (official docs).
The paint can stay the same (color.same → true) while the space name changes from rgb to oklch. That is the core story of space migrations.
Applications
🚀 Real-World Use Cases
Token audits — log which spaces your design tokens currently use.
Mixin branching — choose rgb-safe vs oklch-aware color math.
Migration checks — confirm to-space actually changed the space name.
Debugging — emit --space custom properties in local builds.
Docs / teaching — show hex is rgb, not a separate space.
Pair with is-legacy — name for detail, boolean for quick gates.
🧠 How Compilation Works
1
Write SCSS
Call color.space($color) after @use.
Source
2
Inspect the color
Sass reads which space the value is stored in.
Inspect
3
Return the name
You get an unquoted string such as rgb or oklch.
Name
4
✓
String ships in CSS
Interpolated names become plain identifiers in the stylesheet.
Watch Out
⚠️ Common Pitfalls
Expecting a “hex” space — hex reports rgb.
Confusing rgb with srgb — modern color(srgb …) is not legacy rgb.
Calling on older Dart Sass — needs 1.79+; expect Undefined function earlier.
Looking for a global space() — there is none; use the module form.
Using only space when you need a boolean — prefer is-legacy for yes/no legacy checks.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:color" and color.space() on Dart Sass 1.79+
Compare against unquoted names (== rgb, == oklch)
Pair with is-legacy and to-space during migrations
Emit space names in debug builds when auditing tokens
Upgrade the compiler before shipping modern color helpers
❌ Don’t
Expect the browser to re-run color.space
Assume LibSass/Ruby Sass support this API
Treat srgb as interchangeable with legacy rgb
Ignore Undefined function errors—upgrade instead
Skip documenting which spaces your tokens are supposed to use
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about color.space()
Read the space name—hex is rgb, modern spaces speak for themselves.
5
Core concepts
📝01
Call
space($color)
API
📦02
Module
sass:color
@use
🎨03
Returns
unquoted name
String
⚡04
Hex
reports rgb
Legacy
✓05
Needs
Dart Sass 1.79+
Version
❓ 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.
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".
Did you know?
Official Sass docs show color(xyz-d65 …) reporting the space name xyz—not xyz-d65. The function returns the space family name Sass uses for that color, which is what you compare in if branches and pass around as an unquoted identifier.
color.space() is the space-name reader for modern Sass colors. Use it to audit tokens, branch mixins, and confirm migrations after to-space—on Dart Sass 1.79+.