@@ -19,8 +19,8 @@ export function color(input: CssColorInputLike | Color): string {
1919 } else if ( input instanceof CssNamedColor ) {
2020 return input . name ;
2121 } else if ( typeof input == "object" ) {
22- // with alpha
23- if ( "r" in input && "a" in input ) {
22+ // with alpha (if alpha is 1, use rgb format instead)
23+ if ( "r" in input && "a" in input && input . a !== 1 ) {
2424 const a = safe_alpha_fallback ( validAlphaValue ( input . a ) ) ;
2525 const rgba = input as ICssRGBA ;
2626 const _r = validColorValue ( rgba . r ) ?? 0 ;
@@ -31,6 +31,11 @@ export function color(input: CssColorInputLike | Color): string {
3131 // no alpha
3232 else if ( "r" in input && "a" ! in input ) {
3333 const rgb = input as RGB ;
34+ const named = namedcolor ( rgb ) ;
35+ if ( named ) {
36+ return named ;
37+ }
38+
3439 return `rgb(${ validColorValue ( rgb . r ) ?? 0 } , ${
3540 validColorValue ( rgb . g ) ?? 0
3641 } , ${ validColorValue ( rgb . b ) ?? 0 } )`;
@@ -42,6 +47,29 @@ export function color(input: CssColorInputLike | Color): string {
4247 }
4348}
4449
50+ /**
51+ * rgb value of white, black as named colors
52+ * @param rgb
53+ */
54+ const namedcolor = ( rgb : RGB ) => {
55+ // black
56+ if ( rgb . r === 0 && rgb . g === 0 && rgb . b === 0 ) {
57+ return "black" ;
58+ }
59+ // white
60+ if ( rgb . r === 1 && rgb . g === 1 && rgb . b === 1 ) {
61+ return "white" ;
62+ }
63+ // blue
64+ if ( rgb . r === 0 && rgb . g === 0 && rgb . b === 1 ) {
65+ return "blue" ;
66+ }
67+ // red
68+ if ( rgb . r === 1 && rgb . g === 0 && rgb . b === 0 ) {
69+ return "red" ;
70+ }
71+ } ;
72+
4573const validColorValue = ( f : number ) => {
4674 if ( f === undefined ) {
4775 return ;
0 commit comments