Gradient Part 2 - Linear gradients#2279
Conversation
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
…o gradient-linear
Co-authored-by: Pg Biel <9021226+PgBiel@users.noreply.github.com>
Co-authored-by: bluebear94 <uruwi@protonmail.com>
|
For the first one I have a good idea of what causes it, and the other one is me only giving it to points, and the two end points are the same color 😅 |
|
I just pushed a commit with some tidying up. While doing that, I noticed the following things:
|
I'll do that in a bit, first I'm finishing some fixes for HSV and HSL interpolation
Imo it's fine since it's trying to describe what the coordinate system is relative to. And I agree that the other names are not much better.
I don't mind either way but since I have a lot of operations depending on quadrants (especially with the other types and on text) it's needed at least internally. I can make it private if you want 🤷♂️
The code is correct (doubled checked it like 10 mins ago), it's sin(h) and cos(h) I admit the names are a bit problematic, it will be changed in my commit with corrections for HSL and HSV
I didn't know, will change. |
…o gradient-linear
|
@laurmaedje @reknih I think I have covered everything you mentioned, apart from:
|
|
As discussed on Discord, I changed the way color maps are handled so that they're purely arrays of colors and no longer use functions with a # of stop param |
|
As discussed here and on Discord, presets have been moved to a dedicated space |
|
Also opened an issue on PDF.js for the compatibility issue I found: mozilla/pdf.js#17065 |
|
Thank you so much for all your work on this! 🎉 |
|
Just to note that there is a slight regression in one of the later commits that makes sharp gradients a bit blurry in SVG format, will be fixed soon (probably tonight, it's like a 10 lines fix but I need to go make dinner now) |


Tracking issue: #2282
Changes
This is the second part of gradients, it includes the following changes:
gradienttypegradient.linearconstructor for creating a gradient with the following args:stops(variadic): either a color or a color and a ratio in an arraydir(defaults toltr): the direction in which the ratio goesspace: the color space in which to interpolate the stopsrelative: what is the gradient relative to (see later)Framecan now beFrameKind::SoftorFrameKind::Hard, this is information being kept of simply to determine what is the "container" of an elementblock,box,columns,page,polygon, andframeproduce hard frames, anything else produces a soft framegradienttype:stopsto get the color stops of the gradientspaceto get the interpolation color space of the gradientrelativereturns the "relative-ness" of a gradient (see later)dirreturns the angle of the gradientkindreturns the constructor used to create this gradient (currently onlygradient.linear)samplesamples a color at a given ratiotsamplessamples many colors (variadic) at a series of given ratiostsharpturns the gradient into its sharp variant (see later)repeatrepeats the stops of the gradientntimes, optionally mirror every other repetitionrelativeargumentrepeatmethodsharpmethodmethods.typto add tests to all methods ingradientsize + gradientoperation for creating astrokeAlso adds the following niceties:
quadrantmethod onanglethat allows getting the quadrant in which the angle is, it is used when computing gradients, I figured it was a nice method to expose to the user.Presets
Gradients on text
Gradients on text are currently disabled, they will be included in Part 4 of the gradient series of PRs. Currently, they give an error when someone tries to use them:

Relative-ness
Gradients can be relative to either the shape they are painted on, or to the nearest parent containers. This is controlled by the
relativeargument of the constructors. By default, gradients are relative to the shape they are painted on, unless they are painted on text, in which case they are relative to the parent.The way the parent is determined is as follows:
The following code exemplifies this, if they were not in the same relative coordinate space for their fills, the result would not look uniform and continuous.
Sharp gradient
Additionally, the user can sample the gradient to make a "sharp" variant (i.e with sharp color bars), this is especially useful for creating a list of colors from a color map. Below a result of such a sharp gradient can be seen along with it's non-sharp version:
Aspect ratio correction
Due to the way gradients are handled, they would normally ignore the aspect ratio of the shape and the
dirof the gradient would become incorrect for gradients with a width to height ratio (aspect ratio) of anything but 1. This is handled inangle.rswith a small method. The reason why I mention it is because making this (imo necessary) correction leads to a larger file size as it decrease how well the exported gradient data can be re-used in PDF and SVG export.