Skip to content

Conversation

@melsonic
Copy link
Contributor

@melsonic melsonic commented Apr 15, 2025

Currently, D2 does not validate gradient color stops, it just renders black color in case the gradient colors are invalid.
This PR adds the functionality to validate each color stop & in case it's invalid, it throws an error.

As per D2 website fill & stroke supports css color names & hex colors apart from gradients, however inside gradient D2 supports all color formats (e.g rgb, hsl, etc), hence I used csscolorparser library instead of checking against NamedColors & ColorHexRegex.

fill docs: https://d2lang.com/tour/style#fill

/fixes #2490

Copy link
Collaborator

@alixander alixander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the check is more precise/expected in ValidColor. Like it's got a gradient pattern, but it's just not valid.

@melsonic
Copy link
Contributor Author

Hey @alixander , there's another way to solve this problem i.e. by validating colors during rendering instead of at compile time.
Approach 1 (Current PR):

  • It validates at compile time (faster error feedback).
  • Code changes are limited to gradient.go.
    I know this approach also has cons, like we are parsing gradients twice here(once during compilation & secondly during rendering) & as you might said ValidColor check might not be as expected.

Approach 2 Closed PR(#2491):

  • It validates colors while rendering.
  • Code changes in multiple files + adding error returns in multiple func s.

I went with the first approach since it fails faster and keeps changes contained, but I’d love your thoughts on which one you prefer.

@alixander
Copy link
Collaborator

@melsonic
Copy link
Contributor Author

I meant if you just add your current check to here: https://github.com/terrastruct/d2/blob/master/lib/color/color.go#L514 , it's cleaner

Ah okay, that makes sense.

@melsonic melsonic requested a review from alixander April 18, 2025 15:56
@melsonic melsonic requested a review from alixander April 21, 2025 19:19
Copy link
Collaborator

@alixander alixander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@alixander alixander merged commit 7150096 into terrastruct:master Apr 22, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate gradient color stops

2 participants