Sass
CSS preprocessor

Sass color.to-space() Function

Beginner
⏱️ 13 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:color · Dart Sass 1.79+

What You’ll Learn

color.to-space() belongs to the built-in sass:color module. It converts a color into another color space. This page covers unquoted $space names, missing channels, how it differs from to-gamut, the Dart Sass 1.79+ requirement, and five examples.

01

Concept

Space conversion

02

Module

@use "sass:color"

03

$space

Unquoted name

04

Unique

Changes space

05

Vs

to-gamut()

06

Practice

5 examples

What Is color.to-space()?

To-space means “rewrite this color in another coordinate system.” Official docs: converts $color into the given $space. A hex brand can become oklch, hsl, or display-p3 while usually keeping the same paint. Official fun fact: this is the only Sass color function that returns a color in a different space than the one passed in.

  • color.to-space(#036, oklch) → an oklch(...) twin
  • color.to-space(#036, display-p3)color(display-p3 …)
  • color.to-space(grey, lch)lch(… 0 none) (missing hue)
💡
Beginner tip

Most other helpers (adjust, scale, mix) change channels but return a color in the same space you started with. Use to-space when you explicitly want the space itself to change.

📝 Syntax

Load the color module, then pass a color and an unquoted destination space:

styles.scss
@use "sass:color";

// Dart Sass 1.79+
color.to-space($color, $space)

Parameters

ParameterTypeDescription
$colorColorThe color to convert (required).
$spaceUnquoted stringDestination space name, e.g. oklch, hsl, display-p3, lch, rgb.

Return value

TypeMeaning
ColorThe same paint (when possible) expressed in $space. May be out-of-gamut for narrower destinations.

📦 Loading sass:color

There is no separate global to-space() built-in. Use the module form on Dart Sass 1.79+.

styles.scss
// Recommended — namespaced (Dart Sass 1.79+)
@use "sass:color";
$ok: color.to-space(#036, oklch);

// Optional — bring members into the current namespace
@use "sass:color" as *;
$ok: to-space(#036, oklch);

// Optional — custom namespace
@use "sass:color" as c;
$ok: c.to-space(#036, oklch);
⚠️
Version gate

If Dart Sass reports Undefined function for color.to-space, upgrade to 1.79+.

🎨 Missing Channels & Legacy Destinations

Official docs call out two important behaviors:

  • Conversion can produce missing channels when the source already has a missing analogous channel, or when a channel is powerless in the destination (for example grey → lch with hue none).
  • When $space is a legacy space (rgb / hsl / hwb), Sass never invents a new missing channel—so the result stays compatible with older browsers.

📋 to-space vs to-gamut

These helpers often travel together, but they answer different needs.

HelperJobSpace of the result
color.to-space()Convert into another color spaceThe destination $space
color.to-gamut()Fit a target gamut with a similar lookSame as the input color
💡
Official pairing

Docs note that converting into a narrower gamut with to-space can leave an out-of-gamut color. Follow with to-gamut when you need an in-gamut twin.

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS receives a finished color in the destination space—no color.to-space() call remains.

FeatureDart SassNotes
color.to-space()1.79+Only color helper that changes the returned space
Related color.space()1.79+Read the space name after conversion
Related color.to-gamut()1.79+Map out-of-gamut results after conversion
LibSass / Ruby SassNo modern module color-space API

⚡ Quick Reference

GoalCode
Import color@use "sass:color";
Hex → oklchcolor.to-space(#036, oklch)
Hex → display-p3color.to-space(#036, display-p3)
Grey → lchcolor.to-space(grey, lch)
Check new spacecolor.space(color.to-space($c, oklch))
Visual equalitycolor.same($c, color.to-space($c, oklch))

Examples Gallery

Examples target Dart Sass 1.79+. Open View Compiled CSS to see converted colors (verified against modern Dart Sass; official docs samples noted).

📚 Getting Started

Convert a hex brand into modern and legacy spaces.

Example 1 — Hex Brand into Oklch, HSL, and Display P3

One seed color, three destination spaces.

styles.scss
@use "sass:color";

$brand: #036;

.tokens {
  --oklch: #{color.to-space($brand, oklch)};
  --hsl: #{color.to-space($brand, hsl)};
  --p3: #{color.to-space($brand, display-p3)};
}

How It Works

Each conversion keeps the brand look while changing how channels are stored. display-p3 is useful for wide-gamut monitors; oklch is great for perceptual adjustments afterward.

Example 2 — Grey Converted to LCH (Official Docs)

Powerless hue becomes missing after conversion.

styles.scss
@use "sass:color";

@debug color.to-space(grey, lch); // lch(53.5850134522% 0 none)

.grey {
  color: color.to-space(grey, lch);
}

How It Works

In LCH, a grey has no chroma, so hue is powerless and may be represented as none. Pair with color.is-missing() when you need a boolean check for that hue.

📈 Practical Patterns

Missing-channel carryover, space checks, and gamut follow-up.

Example 3 — Missing Channels Can Carry Across Spaces

Official docs sample: lch(none …) into oklch keeps missing lightness.

styles.scss
@use "sass:color";

@debug color.to-space(lch(none 10% 30deg), oklch);
// oklch(none 0.3782382557 11.1889157942deg)

.carry {
  color: color.to-space(lch(none 10% 30deg), oklch);
}

How It Works

Analogous missing channels travel with the conversion. That is useful for CSS relative-color workflows, and surprising if you expected every channel to become a concrete number.

Example 4 — Confirm Space Change + Visual Equality

Use space and same after converting a hex brand.

styles.scss
@use "sass:color";

$brand: #036;
$brand-oklch: color.to-space($brand, oklch);

.report {
  --before: #{color.space($brand)};
  --after: #{color.space($brand-oklch)};
  --same-paint: #{color.same($brand, $brand-oklch)};
}

How It Works

The space name changes from rgb to oklch, but color.same still reports true—perfect for migration audits.

Example 5 — Convert, Then Gamut-Map if Needed

Convert a wide oklch into rgb space, then map for safer shipping.

styles.scss
@use "sass:color";

$wide: oklch(60% 70% 20deg);
$as-rgb: color.to-space($wide, rgb);
$safe: color.to-gamut($wide, $space: rgb, $method: local-minde);

.pipeline {
  --converted-space: #{color.space($as-rgb)};
  --safe-oklch: #{$safe};
}

How It Works

to-space(..., rgb) changes the stored space to rgb. to-gamut keeps the original oklch space while fitting the rgb gamut—two different tools for two different goals.

🚀 Real-World Use Cases

  • Token migrations — move hex brands into oklch for perceptual edits.
  • Wide-gamut export — convert into display-p3 for modern displays.
  • Legacy output — convert into rgb / hsl before emitting older CSS.
  • Audit pipelines — pair with space and same after conversion.
  • Missing-channel workflows — preserve or detect none after space changes.
  • Teaching color spaces — show that space conversion is not gamut mapping.

🧠 How Compilation Works

1

Write SCSS

Call color.to-space($c, oklch) after @use.

Source
2

Pick destination

Pass an unquoted space name such as display-p3 or hsl.

Space
3

Convert channels

Sass rewrites the color into the destination coordinates.

Convert
4

New-space color ships

CSS receives oklch / hsl / display-p3—not a Sass call.

⚠️ Common Pitfalls

  • Quoting the space name — pass oklch, not "oklch".
  • Confusing with to-gamut — conversion changes space; gamut mapping keeps the input space.
  • Assuming every conversion is in-gamut — narrower destinations may need to-gamut.
  • Surprise none channels — greys and powerless channels can become missing.
  • Calling on older Dart Sass — needs 1.79+; expect Undefined function earlier.

💡 Best Practices

✅ Do

  • Use @use "sass:color" and color.to-space() on Dart Sass 1.79+
  • Verify with color.space() and color.same() during migrations
  • Convert to oklch before perceptual channel edits
  • Follow with to-gamut when targeting a narrower gamut
  • Document which space each design token is supposed to live in

❌ Don’t

  • Expect the browser to re-run color.to-space
  • Treat space conversion as automatic gamut safety
  • Quote space names
  • Assume LibSass/Ruby Sass support this API
  • Ignore missing-channel results on greys and powerless hues

Key Takeaways

Knowledge Unlocked

Five things to remember about color.to-space()

Explicit space conversion—the only helper that changes the returned space.

5
Core concepts
📦 02

Module

sass:color

@use
🎨 03

Does

change color space

Convert
04

$space

unquoted name

Arg
05

Needs

Dart Sass 1.79+

Version

❓ Frequently Asked Questions

color.to-space($color, $space) converts a color into another color space. Example: color.to-space(#036, oklch) returns an oklch color that paints like #036.
Pass an unquoted space name such as oklch, hsl, display-p3, lch, or rgb.
Usually the color looks the same, just stored differently. color.same(#036, color.to-space(#036, oklch)) is true. A narrower destination gamut can leave you out-of-gamut—use color.to-gamut() afterward if needed.
to-space can produce missing channels when a channel is missing on the source or powerless in the destination. Converting into a legacy space never invents a new missing channel.
color.to-space() needs Dart Sass 1.79+. Older compilers report Undefined function.
No. Use the module form color.to-space() after @use "sass:color".
Did you know?

Official Sass docs call color.to-space() the only color function that returns a color in a different space than the one passed in. Every other modern color helper keeps the original space on the way out—even when it temporarily computes in another space.

Conclusion

color.to-space() is the explicit space converter for modern Sass colors. Use it to migrate tokens, unlock perceptual spaces, and pair with to-gamut when the destination gamut is narrower—on Dart Sass 1.79+.

Continue with color.to-gamut(), color.space(), or color.whiteness().

Next: color.whiteness()

You learned color-space conversion with color.to-space()—next up is the deprecated HWB whiteness reader.

color.whiteness() →

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.

5 people found this page helpful