1- import { Injectable } from '@angular/core' ;
1+ import { inject , Injectable } from '@angular/core' ;
22import {
33 ThemeBuilder ,
44 ThemeBuilderContext ,
55 ThemeTokens ,
66} from '@angularity/theming' ;
77import {
8- argbFromHex ,
98 DynamicColor ,
109 DynamicScheme ,
1110 Hct ,
12- hexFromArgb ,
1311 MaterialDynamicColors ,
1412} from '@material/material-color-utilities' ;
1513
14+ import { HctFormatter } from './hct-formatter.service' ;
15+
1616/**
1717 * Mode for Material color schemes.
1818 * @see `SchemeBuilder`
@@ -23,7 +23,8 @@ export enum SchemeMode {
2323}
2424
2525/**
26- * Common contrast level values for Material color schemes.
26+ * Common contrast level values for Material color schemes. `Standard` should
27+ * fit for most use cases.
2728 * @see `SchemeBuilder`
2829 */
2930export enum SchemeContrastLevel {
@@ -69,8 +70,18 @@ export interface SchemeBuilderConfig {
6970 * The source/seed color to use for creating the color scheme. The generated
7071 * color scheme may deviate from this color at some extent, depending on the
7172 * chrome of the source color and the algorithm used by the `SchemeType`.
73+ *
74+ * @remarks `Hct` is the color representation from `material-color-utilities`,
75+ * which consists of hue, chroma, and tone.
76+ *
77+ * @example
78+ * ```ts
79+ * import { Hct, argbFromHex } from '@material/material-color-utilities';
80+ * Hct.fromInt(0xc2e7ff)
81+ * Hct.fromInt(argbFromHex("#c2e7ff"))
82+ * ```
7283 */
73- source : string ;
84+ source : Hct ;
7485
7586 /**
7687 * The mode of the color scheme. Light or Dark.
@@ -91,21 +102,25 @@ export interface SchemeBuilderConfig {
91102 */
92103@Injectable ( { providedIn : 'root' } )
93104export class SchemeBuilder implements ThemeBuilder < SchemeBuilderConfig > {
105+ protected hctFormatter = inject ( HctFormatter ) ;
106+
94107 build ( context : ThemeBuilderContext < SchemeBuilderConfig > ) : ThemeTokens {
95108 const scheme = this . getScheme ( context . config ) ;
96109 const tokens : ThemeTokens = { } ;
97110 for ( const [ k , v ] of Object . entries ( MaterialDynamicColors ) ) {
98111 if ( ! ( v instanceof DynamicColor ) ) continue ;
99112 const name = k . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / gu, '$1-$2' ) . toLowerCase ( ) ;
113+ const color = v . getHct ( scheme ) ;
100114 const token = `${ context . name } -${ name } ` ;
101- tokens [ token ] = hexFromArgb ( v . getArgb ( scheme ) ) ;
115+ const value = this . hctFormatter . format ( color ) ;
116+ tokens [ token ] = value ;
102117 }
103118 return tokens ;
104119 }
105120
106121 protected getScheme ( config : SchemeBuilderConfig ) : DynamicScheme {
107122 return new config . type (
108- Hct . fromInt ( argbFromHex ( config . source ) ) ,
123+ config . source ,
109124 config . mode === SchemeMode . Dark ,
110125 config . contrast ,
111126 ) ;
0 commit comments