@@ -3,7 +3,7 @@ import walk from 'css-tree/walker'
3
3
import { calculateForAST } from '@bramus/specificity/core'
4
4
import { isSupportsBrowserhack , isMediaBrowserhack } from './atrules/atrules.js'
5
5
import { getCombinators , getComplexity , isAccessibility , isPrefixed , hasPseudoClass } from './selectors/utils.js'
6
- import { colorFunctions , colorKeywords , namedColors , systemColors } from './values/colors.js'
6
+ import { colorFunctions , colorKeywords , colorSpace , namedColors , systemColors } from './values/colors.js'
7
7
import { destructure , isSystemFont } from './values/destructure-font-shorthand.js'
8
8
import { isValueKeyword , keywords , isValueReset } from './values/values.js'
9
9
import { analyzeAnimation } from './values/animations.js'
@@ -197,6 +197,7 @@ export function analyze(css, options = {}) {
197
197
let durations = new Collection ( useLocations )
198
198
let colors = new ContextCollection ( useLocations )
199
199
let colorFormats = new Collection ( useLocations )
200
+ let colorSpaces = new Collection ( useLocations )
200
201
let units = new ContextCollection ( useLocations )
201
202
let gradients = new Collection ( useLocations )
202
203
let valueKeywords = new Collection ( useLocations )
@@ -643,6 +644,7 @@ export function analyze(css, options = {}) {
643
644
}
644
645
colors . push ( '#' + valueNode . value , property , loc )
645
646
colorFormats . p ( `hex` + hexLength , loc )
647
+ colorSpaces . p ( 'srgb' , loc )
646
648
647
649
return this . skip
648
650
}
@@ -664,6 +666,7 @@ export function analyze(css, options = {}) {
664
666
let stringified = stringifyNode ( valueNode )
665
667
colors . push ( stringified , property , loc )
666
668
colorFormats . p ( nodeName . toLowerCase ( ) , loc )
669
+ colorSpaces . p ( 'srgb' , loc )
667
670
return
668
671
}
669
672
@@ -672,6 +675,7 @@ export function analyze(css, options = {}) {
672
675
let stringified = stringifyNode ( valueNode )
673
676
colors . push ( stringified , property , loc )
674
677
colorFormats . p ( 'named' , loc )
678
+ colorSpaces . p ( 'srgb' , loc )
675
679
return
676
680
}
677
681
@@ -680,6 +684,7 @@ export function analyze(css, options = {}) {
680
684
let stringified = stringifyNode ( valueNode )
681
685
colors . push ( stringified , property , loc )
682
686
colorFormats . p ( 'system' , loc )
687
+ colorSpaces . p ( 'srgb' , loc )
683
688
return
684
689
}
685
690
return this . skip
@@ -692,8 +697,41 @@ export function analyze(css, options = {}) {
692
697
693
698
// rgb(a), hsl(a), color(), hwb(), lch(), lab(), oklab(), oklch()
694
699
if ( colorFunctions . has ( nodeName ) ) {
695
- colors . push ( stringifyNode ( valueNode ) , property , valueNode . loc )
696
- colorFormats . p ( nodeName . toLowerCase ( ) , valueNode . loc )
700
+ let loc = valueNode . loc
701
+ colors . push ( stringifyNode ( valueNode ) , property , loc )
702
+ colorFormats . p ( nodeName . toLowerCase ( ) , loc )
703
+
704
+ if ( new KeywordSet ( [ 'rgb' , 'rgba' , 'hsl' , 'hsla' , 'hwb' ] ) . has ( nodeName ) ) {
705
+ colorSpaces . p ( 'srgb' , loc )
706
+ } else if ( nodeName . toLowerCase ( ) === 'oklch' ) {
707
+ colorSpaces . p ( 'oklch' , loc )
708
+ } else if ( new KeywordSet ( [ 'lab' , 'lch' ] ) . has ( nodeName ) ) {
709
+ colorSpaces . p ( 'lab' , loc )
710
+ } else if ( nodeName . toLowerCase ( ) === 'oklab' ) {
711
+ colorSpaces . p ( 'oklab' , loc )
712
+ } else if ( nodeName . toLowerCase ( ) === 'color' ) {
713
+ // Determine color space from the color value
714
+ // color(space ...) -> space
715
+ // color(from X Y ...) -> Y
716
+ let space_or_from = valueNode . children . first
717
+ if ( space_or_from . type === 'Identifier' ) {
718
+ if ( space_or_from . name . toLowerCase ( ) === 'from' ) {
719
+ // Take the next identifier as the color space
720
+ let next = space_or_from . next
721
+ if ( next ?. type === 'Identifier' ) {
722
+ let space = colorSpace ( next . name )
723
+ if ( space ) {
724
+ colorSpaces . p ( space , loc )
725
+ }
726
+ }
727
+ } else {
728
+ let space = colorSpace ( space_or_from . name )
729
+ if ( space ) {
730
+ colorSpaces . p ( space , loc )
731
+ }
732
+ }
733
+ }
734
+ }
697
735
return
698
736
}
699
737
@@ -1012,6 +1050,7 @@ export function analyze(css, options = {}) {
1012
1050
colors . count ( ) ,
1013
1051
{
1014
1052
formats : colorFormats . c ( ) ,
1053
+ spaces : colorSpaces . c ( ) ,
1015
1054
} ,
1016
1055
) ,
1017
1056
gradients : gradients . c ( ) ,
0 commit comments