1- import { type Color , modeLrgb , modeRgb , parse , type Rgb , useMode , wcagLuminance } from "culori/fn" ;
2- import { Shadow , StyleProperty } from "@adaptive-web/adaptive-ui" ;
1+ import { type Color as CuloriColor , modeLrgb , modeRgb , parse , type Rgb , useMode , wcagLuminance } from "culori/fn" ;
2+ import { Color , Shadow , StyleProperty } from "@adaptive-web/adaptive-ui" ;
33import { AppliedStyleModules , AppliedStyleValues , Controller , focusIndicatorNodeName , PluginNode , PluginNodeData , State , StatesState , STYLE_REMOVE } from "@adaptive-web/adaptive-ui-designer-core" ;
44import { FIGMA_SHARED_DATA_NAMESPACE } from "@adaptive-web/adaptive-ui-designer-figma" ;
5- import { colorToRgba , variantBooleanHelper } from "./utility.js" ;
5+ import { colorToRgba , roundToDecimals , variantBooleanHelper } from "./utility.js" ;
66
77const rgb = useMode ( modeRgb ) ;
88// For luminance
@@ -119,7 +119,7 @@ export class FigmaPluginNode extends PluginNode {
119119 public id : string ;
120120 public type : string ;
121121 public name : string ;
122- public fillColor : Color | null = null ;
122+ public fillColor : CuloriColor | null = null ;
123123 public states ?: StatesState ;
124124 private _node : BaseNode ;
125125 private _state ?: State ;
@@ -760,7 +760,7 @@ export class FigmaPluginNode extends PluginNode {
760760 return await FigmaPluginNode . get ( parent , false ) ;
761761 }
762762
763- private getFillColor ( ) : Color | null {
763+ private getFillColor ( ) : CuloriColor | null {
764764 // console.log("FigmaPluginNode.getFillColor", this.debugInfo);
765765 if ( ( this . _node as GeometryMixin ) . fills ) {
766766 const fills = ( this . _node as GeometryMixin ) . fills ;
@@ -783,7 +783,7 @@ export class FigmaPluginNode extends PluginNode {
783783 return null ;
784784 }
785785
786- public async getEffectiveFillColor ( ) : Promise < Color | null > {
786+ public async getEffectiveFillColor ( ) : Promise < CuloriColor | null > {
787787 if ( this . fillColor ) {
788788 return this . fillColor ;
789789 }
@@ -849,90 +849,109 @@ export class FigmaPluginNode extends PluginNode {
849849 }
850850 }
851851
852- private paintColor ( target : StyleProperty , value : string ) : void {
852+ private paintColor ( target : StyleProperty , value : unknown ) : void {
853853 let paint : Paint | null = null ;
854854
855855 if ( value !== STYLE_REMOVE ) {
856- if ( value . startsWith ( "linear-gradient" ) ) {
857- const linearMatch = / l i n e a r - g r a d i e n t \( (?< params > .+ ) \) / ;
858- const matches = value . match ( linearMatch ) ;
859- if ( matches && matches . groups ) {
860- const array = matches . groups . params . split ( "," ) . map ( p => p . trim ( ) ) ;
861-
862- let degrees : number = 90 ;
863- if ( array [ 0 ] . endsWith ( "deg" ) ) {
864- const angle = array . shift ( ) ?. replace ( "deg" , "" ) || "90" ;
865- degrees = Number . parseFloat ( angle ) ;
866- }
867- const radians : number = degrees * ( Math . PI / 180 ) ;
868-
869- const paramMatch = / (?< color > # [ \w \d ] + ) ( (?< pos > .+ ) ) ? / ;
870- const stops = array . map ( ( p , index , array ) => {
871- const paramMatches = p . match ( paramMatch ) ;
872- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
873- const color = rgb ( parse ( paramMatches ?. groups ?. color || "FF00FF" ) ! ) ;
874- let position : number = 0 ;
875- if ( paramMatches ?. groups && paramMatches ?. groups ?. pos ) {
876- if ( paramMatches . groups . pos . endsWith ( "%" ) ) {
877- position = Number . parseFloat ( paramMatches . groups . pos ) / 100 ;
878- } else if ( paramMatches . groups . pos . startsWith ( "calc(100% - " ) ) {
879- const px = Number . parseFloat (
880- paramMatches . groups . pos
881- . replace ( "calc(100% - " , "" )
882- . replace ( "px)" , "" )
883- ) ;
884- const size = degrees === 90 || degrees === 270
885- ? ( this . _node as LayoutMixin ) . height
886- : ( this . _node as LayoutMixin ) . width ;
887- position = ( size - px ) / size ;
888- }
889- } else if ( index === array . length - 1 ) {
890- position = 1 ;
856+ if ( typeof value === "string" ) {
857+ if ( value . startsWith ( "linear-gradient" ) ) {
858+ const linearMatch = / l i n e a r - g r a d i e n t \( (?< params > .+ ) \) / ;
859+ const matches = value . match ( linearMatch ) ;
860+ if ( matches && matches . groups ) {
861+ const array = matches . groups . params . split ( "," ) . map ( p => p . trim ( ) ) ;
862+
863+ let degrees : number = 90 ;
864+ if ( array [ 0 ] . endsWith ( "deg" ) ) {
865+ const angle = array . shift ( ) ?. replace ( "deg" , "" ) || "90" ;
866+ degrees = Number . parseFloat ( angle ) ;
891867 }
892- const stop : ColorStop = {
893- position,
894- color : {
895- r : color . r ,
896- g : color . g ,
897- b : color . b ,
898- a : color . alpha || 1 ,
899- } ,
868+ const radians : number = degrees * ( Math . PI / 180 ) ;
869+
870+ const paramMatch = / (?< color > # [ \w \d ] + ) ( (?< pos > .+ ) ) ? / ;
871+ const stops = array . map ( ( p , index , array ) => {
872+ const paramMatches = p . match ( paramMatch ) ;
873+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
874+ const color = rgb ( parse ( paramMatches ?. groups ?. color || "FF00FF" ) ! ) ;
875+ let position : number = 0 ;
876+ if ( paramMatches ?. groups && paramMatches ?. groups ?. pos ) {
877+ if ( paramMatches . groups . pos . endsWith ( "%" ) ) {
878+ position = Number . parseFloat ( paramMatches . groups . pos ) / 100 ;
879+ } else if ( paramMatches . groups . pos . startsWith ( "calc(100% - " ) ) {
880+ const px = Number . parseFloat (
881+ paramMatches . groups . pos
882+ . replace ( "calc(100% - " , "" )
883+ . replace ( "px)" , "" )
884+ ) ;
885+ const size = degrees === 90 || degrees === 270
886+ ? ( this . _node as LayoutMixin ) . height
887+ : ( this . _node as LayoutMixin ) . width ;
888+ position = ( size - px ) / size ;
889+ }
890+ } else if ( index === array . length - 1 ) {
891+ position = 1 ;
892+ }
893+ const stop : ColorStop = {
894+ position,
895+ color : {
896+ r : color . r ,
897+ g : color . g ,
898+ b : color . b ,
899+ a : color . alpha || 1 ,
900+ } ,
901+ } ;
902+ return stop ;
903+ } ) ;
904+
905+ const gradientPaint : GradientPaint = {
906+ type : "GRADIENT_LINEAR" ,
907+ gradientStops : stops ,
908+ gradientTransform : [
909+ [ Math . cos ( radians ) , Math . sin ( radians ) , 0 ] ,
910+ [ Math . sin ( radians ) * - 1 , Math . cos ( radians ) , 1 ] ,
911+ ] ,
900912 } ;
901- return stop ;
902- } ) ;
903-
904- const gradientPaint : GradientPaint = {
905- type : "GRADIENT_LINEAR" ,
906- gradientStops : stops ,
907- gradientTransform : [
908- [ Math . cos ( radians ) , Math . sin ( radians ) , 0 ] ,
909- [ Math . sin ( radians ) * - 1 , Math . cos ( radians ) , 1 ] ,
910- ] ,
913+ paint = gradientPaint ;
914+ }
915+ } else {
916+ // Assume it's solid
917+ const color = parse ( value ) ;
918+ if ( ! color ) {
919+ throw new Error (
920+ `The value "${ value } " could not be parsed`
921+ ) ;
922+ }
923+
924+ const rgbColor = rgb ( color ) ;
925+ const solidPaint : SolidPaint = {
926+ type : "SOLID" ,
927+ visible : true ,
928+ opacity : rgbColor . alpha ,
929+ blendMode : "NORMAL" ,
930+ color : {
931+ r : rgbColor . r ,
932+ g : rgbColor . g ,
933+ b : rgbColor . b ,
934+ } ,
911935 } ;
912- paint = gradientPaint ;
936+ paint = solidPaint ;
913937 }
914- } else {
915- // Assume it's solid
916- const color = parse ( value ) ;
917- if ( ! color ) {
918- throw new Error (
919- `The value "${ value } " could not be parsed`
920- ) ;
938+ } else if ( value && typeof value === "object" ) {
939+ if ( Object . keys ( value ) . includes ( "color" ) ) {
940+ const color = value as Color ;
941+ // console.log(" Color", color);
942+ const solidPaint : SolidPaint = {
943+ type : "SOLID" ,
944+ visible : true ,
945+ opacity : color . color . alpha ,
946+ blendMode : "NORMAL" ,
947+ color : {
948+ r : roundToDecimals ( ( color . color as Rgb ) . r , 6 ) ,
949+ g : roundToDecimals ( ( color . color as Rgb ) . g , 6 ) ,
950+ b : roundToDecimals ( ( color . color as Rgb ) . b , 6 ) ,
951+ } ,
952+ } ;
953+ paint = solidPaint ;
921954 }
922-
923- const rgbColor = rgb ( color ) ;
924- const solidPaint : SolidPaint = {
925- type : "SOLID" ,
926- visible : true ,
927- opacity : rgbColor . alpha ,
928- blendMode : "NORMAL" ,
929- color : {
930- r : rgbColor . r ,
931- g : rgbColor . g ,
932- b : rgbColor . b ,
933- } ,
934- } ;
935- paint = solidPaint ;
936955 }
937956 }
938957
0 commit comments