bostrom/text-to-image

An invalid text color gives trasparent text.

Closed this issue · 6 comments

An invalid text color gives transparent text, can you default it to black or something.
Thx a lot for that nice package.

image

Thanks for the report. Makes sense, I'll see what I can do.

@iMrDJAi what was the invalid value you tried to use which produced transparent text?

Defaulting to black is a bit trickier than it sounds, since

The CanvasRenderingContext2D.fillStyle property of the Canvas 2D API specifies the color, gradient, or pattern to use inside shapes. The default style is #000 (black).

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle

The color value alone can be a color name, a hex value, an rgb or rgba value, so validating that input would be too laborious and brittle.

So as I see it, the color value validation should happen before entering into this library, since you'll have control over the rules for which colors are valid.

@iMrDJAi what was the invalid value you tried to use which produced transparent text?

Defaulting to black is a bit trickier than it sounds, since

The CanvasRenderingContext2D.fillStyle property of the Canvas 2D API specifies the color, gradient, or pattern to use inside shapes. The default style is #000 (black).

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle

The color value alone can be a color name, a hex value, an rgb or rgba value, so validating that input would be too laborious and brittle.

So as I see it, the color value validation should happen before entering into this library, since you'll have control over the rules for which colors are valid.

Generally the color name, I know it supports HTML 5 colors, hex (I didn't try rgb/rgba).
So yeah I think regex can handle this, since there is nothing to do with the Canvas 2D API.
I will try to make it, then maybe I gonna do a pull request to add it here, thx anyway.

I guess a fairly ok solution would a regex validation on color values that are strings. Patterns, gradients etc are all objects, so validation can be skipped for those.

But my opinion is still that validation should happen at the caller, not in this library.

However, PRs are welcome and a good way to continue the discussion.

I changed my mind, a regex solution needs a ton of work, there are so many formats of colors, each one of them needs a separate checker, so an implementation like this can't be reliable.
A better solution is looking up for an open source library that does the job for you, this will prevent unnecessary complications.
By the way, I found this one and it seems good for the purpose: validate-color.

As discussed in #145, I improved the README in 6f2f4a2.