| 1 | # PostCSS OKLab Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
|
|---|
| 2 |
|
|---|
| 3 | [<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-oklab-function.svg" height="20">][npm-url]
|
|---|
| 4 | [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/oklab-function.svg" height="20">][css-url]
|
|---|
| 5 | [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url]
|
|---|
| 6 | [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 7 |
|
|---|
| 8 | [PostCSS OKLab Function] lets you use `oklab` and `oklch` color functions in
|
|---|
| 9 | CSS, following the [CSS Color] specification.
|
|---|
| 10 |
|
|---|
| 11 | ```pcss
|
|---|
| 12 | .test-oklab {
|
|---|
| 13 | color: oklab(40% 0.001236 0.0039);
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | .test-oklch {
|
|---|
| 17 | color: oklch(40% 0.268735435 34.568626);
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | /* becomes */
|
|---|
| 21 |
|
|---|
| 22 | .test-oklab {
|
|---|
| 23 | color: rgb(73, 71, 69);
|
|---|
| 24 | color: color(display-p3 0.28515 0.27983 0.27246);
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | .test-oklch {
|
|---|
| 28 | color: rgb(131, 28, 0);
|
|---|
| 29 | color: color(display-p3 0.49163 0.11178 0.00000);
|
|---|
| 30 | }
|
|---|
| 31 | ```
|
|---|
| 32 |
|
|---|
| 33 | ## Usage
|
|---|
| 34 |
|
|---|
| 35 | Add [PostCSS OKLab Function] to your project:
|
|---|
| 36 |
|
|---|
| 37 | ```bash
|
|---|
| 38 | npm install postcss @csstools/postcss-oklab-function --save-dev
|
|---|
| 39 | ```
|
|---|
| 40 |
|
|---|
| 41 | Use it as a [PostCSS] plugin:
|
|---|
| 42 |
|
|---|
| 43 | ```js
|
|---|
| 44 | const postcss = require('postcss');
|
|---|
| 45 | const postcssOKLabFunction = require('@csstools/postcss-oklab-function');
|
|---|
| 46 |
|
|---|
| 47 | postcss([
|
|---|
| 48 | postcssOKLabFunction(/* pluginOptions */)
|
|---|
| 49 | ]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | [PostCSS OKLab Function] runs in all Node environments, with special
|
|---|
| 53 | instructions for:
|
|---|
| 54 |
|
|---|
| 55 | | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|---|
| 56 | | --- | --- | --- | --- | --- | --- |
|
|---|
| 57 |
|
|---|
| 58 | ## Options
|
|---|
| 59 |
|
|---|
| 60 | ### preserve
|
|---|
| 61 |
|
|---|
| 62 | The `preserve` option determines whether the original notation
|
|---|
| 63 | is preserved. By default, it is not preserved.
|
|---|
| 64 |
|
|---|
| 65 | ```js
|
|---|
| 66 | postcssOKLabFunction({ preserve: true })
|
|---|
| 67 | ```
|
|---|
| 68 |
|
|---|
| 69 | ```pcss
|
|---|
| 70 | .test-oklab {
|
|---|
| 71 | color: oklab(40% 0.001236 0.0039);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | .test-oklch {
|
|---|
| 75 | color: oklch(40% 0.268735435 34.568626);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /* becomes */
|
|---|
| 79 |
|
|---|
| 80 | .test-oklab {
|
|---|
| 81 | color: rgb(73, 71, 69);
|
|---|
| 82 | color: color(display-p3 0.28515 0.27983 0.27246);
|
|---|
| 83 | color: oklab(40% 0.001236 0.0039);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | .test-oklch {
|
|---|
| 87 | color: rgb(131, 28, 0);
|
|---|
| 88 | color: color(display-p3 0.49163 0.11178 0.00000);
|
|---|
| 89 | color: oklch(40% 0.268735435 34.568626);
|
|---|
| 90 | }
|
|---|
| 91 | ```
|
|---|
| 92 |
|
|---|
| 93 | ### enableProgressiveCustomProperties
|
|---|
| 94 |
|
|---|
| 95 | The `enableProgressiveCustomProperties` option determines whether the original notation
|
|---|
| 96 | is wrapped with `@supports` when used in Custom Properties. By default, it is enabled.
|
|---|
| 97 |
|
|---|
| 98 | ⚠️ We only recommend disabling this when you set `preserve` to `false` or if you bring your own fix for Custom Properties. See what the plugin does in its [README](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties#readme).
|
|---|
| 99 |
|
|---|
| 100 | ```js
|
|---|
| 101 | postcssOKLabFunction({ enableProgressiveCustomProperties: false })
|
|---|
| 102 | ```
|
|---|
| 103 |
|
|---|
| 104 | ```pcss
|
|---|
| 105 | :root {
|
|---|
| 106 | --firebrick: oklab(40% 0.234 0.39);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /* becomes */
|
|---|
| 110 |
|
|---|
| 111 | :root {
|
|---|
| 112 | --firebrick: rgb(133, 0, 67); /* will never be used, not even in older browser */
|
|---|
| 113 | --firebrick: color(display-p3 0.49890 0.00000 0.25954); /* will never be used, not even in older browser */
|
|---|
| 114 | --firebrick: oklab(40% 0.234 0.39);
|
|---|
| 115 | }
|
|---|
| 116 | ```
|
|---|
| 117 |
|
|---|
| 118 | ### subFeatures
|
|---|
| 119 |
|
|---|
| 120 | #### displayP3
|
|---|
| 121 |
|
|---|
| 122 | The `subFeatures.displayP3` option determines if `color(display-p3 ...)` is used as a fallback.<br>
|
|---|
| 123 | By default, it is enabled.
|
|---|
| 124 |
|
|---|
| 125 | `display-p3` can display wider gamut colors than `rgb` on some devices.
|
|---|
| 126 |
|
|---|
| 127 | ```js
|
|---|
| 128 | postcssOKLabFunction({
|
|---|
| 129 | subFeatures: {
|
|---|
| 130 | displayP3: false
|
|---|
| 131 | }
|
|---|
| 132 | })
|
|---|
| 133 | ```
|
|---|
| 134 |
|
|---|
| 135 | ```pcss
|
|---|
| 136 | .test-oklab {
|
|---|
| 137 | color: oklab(40% 0.001236 0.0039);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | .test-oklch {
|
|---|
| 141 | color: oklch(40% 0.268735435 34.568626);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /* becomes */
|
|---|
| 145 |
|
|---|
| 146 | .test-oklab {
|
|---|
| 147 | color: rgb(73, 71, 69);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | .test-oklch {
|
|---|
| 151 | color: rgb(131, 28, 0);
|
|---|
| 152 | }
|
|---|
| 153 | ```
|
|---|
| 154 |
|
|---|
| 155 | ## Out of gamut colors
|
|---|
| 156 |
|
|---|
| 157 | Depending on the browser implementation out of gamut colors may be clipped, resulting in a different color.<br>
|
|---|
| 158 | Fallback values generated by [PostCSS OKLab Function] are always mapped to a close alternative in sRGB.
|
|---|
| 159 |
|
|---|
| 160 | When setting `preserve` to `true` the original values will be used by some browsers and these may be clipped.<br>
|
|---|
| 161 | Certain browsers will have an incorrect color if this occurs.
|
|---|
| 162 |
|
|---|
| 163 | If the plugin detects out of gamut colors it will emit a warning :
|
|---|
| 164 |
|
|---|
| 165 | > "oklab(40% 0.234 0.39)" is out of gamut for "display-p3". When "preserve: true" is set this will lead to unexpected results in some browsers.
|
|---|
| 166 |
|
|---|
| 167 | To resolve this warning you can use a color that is in gamut for `display-p3`.
|
|---|
| 168 |
|
|---|
| 169 | ## Copyright : color conversions
|
|---|
| 170 |
|
|---|
| 171 | This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/tree/main/css-color-4. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
|
|---|
| 172 |
|
|---|
| 173 | [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|---|
| 174 | [css-url]: https://cssdb.org/#oklab-function
|
|---|
| 175 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 176 | [npm-url]: https://www.npmjs.com/package/@csstools/postcss-oklab-function
|
|---|
| 177 |
|
|---|
| 178 | [CSS Color]: https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch
|
|---|
| 179 | [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
|
|---|
| 180 | [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
|
|---|
| 181 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 182 | [PostCSS Loader]: https://github.com/postcss/postcss-loader
|
|---|
| 183 | [PostCSS OKLab Function]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-oklab-function
|
|---|