@@ -182,6 +182,7 @@ pub struct RenderParams {
182182 pub alignment_parent_transform : Option < DAffine2 > ,
183183 pub aligned_strokes : bool ,
184184 pub override_paint_order : bool ,
185+ pub artboard_background : Option < Color > ,
185186}
186187
187188impl Hash for RenderParams {
@@ -198,6 +199,7 @@ impl Hash for RenderParams {
198199 }
199200 self . aligned_strokes . hash ( state) ;
200201 self . override_paint_order . hash ( state) ;
202+ self . artboard_background . hash ( state) ;
201203 }
202204}
203205
@@ -235,6 +237,16 @@ fn max_scale(transform: DAffine2) -> f64 {
235237 ( sx + sy) . sqrt ( )
236238}
237239
240+ pub fn contrasting_outline_color ( background : Option < Color > ) -> Color {
241+ use core_types:: consts:: LAYER_OUTLINE_STROKE_COLOR ;
242+ if let Some ( bg) = background {
243+ let luminance = 0.2126 * bg. r ( ) + 0.7152 * bg. g ( ) + 0.0722 * bg. b ( ) ;
244+ if luminance > 0.5 { Color :: BLACK } else { Color :: WHITE }
245+ } else {
246+ LAYER_OUTLINE_STROKE_COLOR
247+ }
248+ }
249+
238250pub fn to_transform ( transform : DAffine2 ) -> usvg:: Transform {
239251 let cols = transform. to_cols_array ( ) ;
240252 usvg:: Transform :: from_row ( cols[ 0 ] as f32 , cols[ 1 ] as f32 , cols[ 2 ] as f32 , cols[ 3 ] as f32 , cols[ 4 ] as f32 , cols[ 5 ] as f32 )
@@ -440,7 +452,9 @@ impl Render for Artboard {
440452 } ,
441453 // Artwork content
442454 |render| {
443- self . content . render_svg ( render, render_params) ;
455+ let mut render_params = render_params. clone ( ) ;
456+ render_params. artboard_background = Some ( self . background ) ;
457+ self . content . render_svg ( render, & render_params) ;
444458 } ,
445459 ) ;
446460 }
@@ -462,7 +476,9 @@ impl Render for Artboard {
462476 }
463477 // Since the content's transform is right multiplied in when rendering the content, we just need to right multiply by the artboard offset here.
464478 let child_transform = transform * DAffine2 :: from_translation ( self . location . as_dvec2 ( ) ) ;
465- self . content . render_to_vello ( scene, child_transform, context, render_params) ;
479+ let mut render_params = render_params. clone ( ) ;
480+ render_params. artboard_background = Some ( self . background ) ;
481+ self . content . render_to_vello ( scene, child_transform, context, & render_params) ;
466482 if self . clip {
467483 scene. pop_layer ( ) ;
468484 }
@@ -881,7 +897,7 @@ impl Render for Table<Vector> {
881897 }
882898
883899 fn render_to_vello ( & self , scene : & mut Scene , parent_transform : DAffine2 , _context : & mut RenderContext , render_params : & RenderParams ) {
884- use core_types:: consts:: { LAYER_OUTLINE_STROKE_COLOR , LAYER_OUTLINE_STROKE_WEIGHT } ;
900+ use core_types:: consts:: LAYER_OUTLINE_STROKE_WEIGHT ;
885901 use graphic_types:: vector_types:: vector:: style:: { GradientType , StrokeCap , StrokeJoin } ;
886902 use vello:: kurbo:: { Cap , Join } ;
887903 use vello:: peniko;
@@ -1065,12 +1081,9 @@ impl Render for Table<Vector> {
10651081 dash_pattern : Default :: default ( ) ,
10661082 dash_offset : 0. ,
10671083 } ;
1068- let outline_color = peniko:: Color :: new ( [
1069- LAYER_OUTLINE_STROKE_COLOR . r ( ) ,
1070- LAYER_OUTLINE_STROKE_COLOR . g ( ) ,
1071- LAYER_OUTLINE_STROKE_COLOR . b ( ) ,
1072- LAYER_OUTLINE_STROKE_COLOR . a ( ) ,
1073- ] ) ;
1084+
1085+ let outline_color = contrasting_outline_color ( render_params. artboard_background ) ;
1086+ let outline_color = peniko:: Color :: new ( [ outline_color. r ( ) , outline_color. g ( ) , outline_color. b ( ) , outline_color. a ( ) ] ) ;
10741087
10751088 scene. stroke ( & outline_stroke, kurbo:: Affine :: new ( element_transform. to_cols_array ( ) ) , outline_color, None , & path) ;
10761089 }
0 commit comments