linearGradients

Discussion about writing code for Inkscape.
JimJoyce
Posts: 37
Joined: Fri Jun 19, 2009 7:06 pm

linearGradients

Postby JimJoyce » Fri Nov 22, 2013 6:35 am

I've just started experimenting with linearGradients.
I tried to display a spectrum of colours and am somewhat disappointed with the result.
I expected a smooth transition but found it rather lumpy.
Am I doing something wrong?

Code: Select all

<svg xmlns="http://www.w3.org/2000/svg"
   version="1.1" baseProfile="full"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   width="1300px" height="250px"
   viewBox="0 0 1300 250">
   
   <defs>
      <rect id="bx" x="0" y="0" width="150" height="100" />

      <linearGradient id="r" >
         <stop offset="0.0"  style="stop-color: #ff0000;" />
         <stop offset="1.0"  style="stop-color: #ffff00;" />
      </linearGradient>
      <linearGradient id="y" >
         <stop offset="0.0"  style="stop-color: #ffff00;" />
         <stop offset="1.0"  style="stop-color: #00ff00;" />
      </linearGradient>
      <linearGradient id="g" >
         <stop offset="0.0"  style="stop-color: #00ff00;" />
         <stop offset="1.0"  style="stop-color: #00ffff;" />
      </linearGradient>
      <linearGradient id="c" >
         <stop offset="0.0"  style="stop-color: #00ffff;" />
         <stop offset="1.0"  style="stop-color: #0000ff;" />
      </linearGradient>
      <linearGradient id="b" >
         <stop offset="0.0"  style="stop-color: #0000ff;" />
         <stop offset="1.0"  style="stop-color: #ff00ff;" />
      </linearGradient>
      <linearGradient id="m" >
         <stop offset="0.0"  style="stop-color: #ff00ff;" />
         <stop offset="1.0"  style="stop-color: #ff0000;" />
      </linearGradient>

   </defs>

    <g id="spectrum">
      <use xlink:href="#bx" x=  "50" y="50" style="fill: url(#m);" />
      <use xlink:href="#bx" x= "200" y="50" style="fill: url(#r);" />
      <use xlink:href="#bx" x= "350" y="50" style="fill: url(#y);" />
      <use xlink:href="#bx" x= "500" y="50" style="fill: url(#g);" />
      <use xlink:href="#bx" x= "650" y="50" style="fill: url(#c);" />
      <use xlink:href="#bx" x= "800" y="50" style="fill: url(#b);" />
      <use xlink:href="#bx" x= "950" y="50" style="fill: url(#m);" />
      <use xlink:href="#bx" x="1100" y="50" style="fill: url(#r);" />

    <path d="M 50,180 L 50,200  1250,200 1250,180"
         style="stroke: #000; stroke-width: 2; fill: none;" />

      <text x="50"    y="190" style="font-Famiy: Arial; font-size; 9;">M</text>
      <text x="195"  y="190" style="font-Famiy: Arial; font-size; 9;">R</text>
      <text x="345"  y="190" style="font-Famiy: Arial; font-size; 9;">Y</text>
      <text x="495"  y="190" style="font-Famiy: Arial; font-size; 9;">G</text>
      <text x="645"  y="190" style="font-Famiy: Arial; font-size; 9;">C</text>
      <text x="795"  y="190" style="font-Famiy: Arial; font-size; 9;">B</text>
      <text x="944"  y="190" style="font-Famiy: Arial; font-size; 9;">M</text>
      <text x="1095" y="190" style="font-Famiy: Arial; font-size; 9;">R</text>
      <text x="1239" y="190" style="font-Famiy: Arial; font-size; 9;">Y</text>

   </g>

</svg>

v1nce
Posts: 696
Joined: Wed Jan 13, 2010 4:36 am

Re: linearGradients

Postby v1nce » Fri Nov 22, 2013 7:21 am

Maybe have a look at http://tavmjong.free.fr/blog/?p=765 about linear rgb vs srgb colorspaces.

I'm not sure about what you mean by lumpy, if you're talking about discontinuities between your rect it's because of "bugs" in the renderer, make yor rect just a little bit wider so that they overlap a bit

JimJoyce
Posts: 37
Joined: Fri Jun 19, 2009 7:06 pm

Re: linearGradients

Postby JimJoyce » Fri Nov 22, 2013 9:45 pm

Not a Reply, just a footnote:

I suspect the problem lies at the join of two gradients.
So, below the original spectrum, I've added another whibh bridges those joins.
I've added 6 more <def>s

Code: Select all

<linearGradient id="mr" >
         <stop offset="0.0"  style="stop-color: #ff0080;" />
         <stop offset="1.0"  style="stop-color: #ff8000;" />
      </linearGradient>
      <linearGradient id="ry" >
         <stop offset="0.0"  style="stop-color: #ff8000;" />
         <stop offset="1.0"  style="stop-color: #80ff00;" />
      </linearGradient>
      <linearGradient id="yg" >
         <stop offset="0.0"  style="stop-color: #80ff00;" />
         <stop offset="1.0"  style="stop-color: #00ff80;" />
      </linearGradient>
      <linearGradient id="gc" >
         <stop offset="0.0"  style="stop-color: #00ff80;" />
         <stop offset="1.0"  style="stop-color: #0080ff;" />
      </linearGradient>
      <linearGradient id="cb" >
         <stop offset="0.0"  style="stop-color: #0080ff;" />
         <stop offset="1.0"  style="stop-color: #8000ff;" />
      </linearGradient>
      <linearGradient id="bm" >
         <stop offset="0.0"  style="stop-color: #8000ff;" />
         <stop offset="1.0"  style="stop-color: #ff0080;" />
      </linearGradient>

and 7 more <use>s.

Code: Select all

      <use xlink:href="#bx" x="125" y="250" style="fill: url(#mr);" />
      <use xlink:href="#bx" x="275" y="250" style="fill: url(#ry);" />
      <use xlink:href="#bx" x="425" y="250" style="fill: url(#yg);" />
      <use xlink:href="#bx" x="575" y="250" style="fill: url(#gc);" />
      <use xlink:href="#bx" x="725" y="250" style="fill: url(#cb);" />
      <use xlink:href="#bx" x="875" y="250" style="fill: url(#bm);" />

I increased the svg height to 450.

The spectrum now looks smoother. The Yellow, Cyan and Magenta are less vivid,
and I cannot see any degradation of the Red, Green and Blue.

But that hardly explains the lumpiness of the original spectrum.
Any comments?, or advice?
Thanks, JJ

Lazur
Posts: 4705
Joined: Tue Jun 14, 2016 10:38 am

Re: linearGradients

Postby Lazur » Sat Nov 23, 2013 11:47 am

Hi. Made a test image how increasing the number of the steps would change the look of such spectrum:
-click on it for full size svg-
SVG Image

The strange thing is, that in most cases opensuse's image viewer renders a smoother svg, but it can not display this one -produces a vibrating mess instead.

Lazur
Posts: 4705
Joined: Tue Jun 14, 2016 10:38 am

Re: linearGradients

Postby Lazur » Sat Nov 23, 2013 2:12 pm

Made another svg.
Linear gradient with 256 steps. Some rendering problems may appear with it too.

SVG Image

JimJoyce
Posts: 37
Joined: Fri Jun 19, 2009 7:06 pm

Re: linearGradients

Postby JimJoyce » Sat Nov 23, 2013 10:26 pm

Hi, Lazur,
Both are stunning..
I cannot imagine how you developed that chunk of code. 18000 lines !
Even the second one, 256 steps you say. I note over 500 Stops.
But that said. They are stunning.
Thanks,
JJ

tylerdurden
Posts: 2322
Joined: Sun Apr 14, 2013 12:04 pm
Location: Michigan, USA

Re: linearGradients

Postby tylerdurden » Sat Nov 23, 2013 11:13 pm

Openclipart is not working for me. :(


JimJoyce wrote:...
I cannot imagine how you developed that chunk of code. 18000 lines !

It might be helpful to note that since svg is text, complex or large patterns can easily be processed in a spreadsheet and pasted into the xml file. (Scripting is also an option.)
Have a nice day.

I'm using Inkscape 0.92.2 (5c3e80d, 2017-08-06), 64 bit win8.1

The Inkscape manual has lots of helpful info! http://tavmjong.free.fr/INKSCAPE/MANUAL/html/

Lazur
Posts: 4705
Joined: Tue Jun 14, 2016 10:38 am

Re: linearGradients

Postby Lazur » Sun Nov 24, 2013 1:20 am

JimJoyce wrote:Hi, Lazur,
Both are stunning..
I cannot imagine how you developed that chunk of code. 18000 lines !
Even the second one, 256 steps you say. I note over 500 Stops.
But that said. They are stunning.
Thanks,
JJ


Glad you like it!

Actually those were drawn manually in inkscape.
Took some time but I was curious.

Though I'm still curious how the second one could be done in other ways.
I added many steps to one gradient, and snapped them to guidelines.
That way I could keep them in right position.
By the edit gradient panel, step positions can be defined in 0,00-1,00 values,
and defining 256 even distances is impossible accurately that way. Or is it?

tylerdurden wrote:Openclipart is not working for me. :(
...


I think it's the renderer in your browser that might not work well, not the page.
Firefox renders a blank page for both, chrome renders but without a smooth transition in the second, and with a break at the cyan values.
The built-in opensuse imageviewer renders something buggy as a pattern fill from a clipped group of objects in the current stabile inkscape window.

tylerdurden
Posts: 2322
Joined: Sun Apr 14, 2013 12:04 pm
Location: Michigan, USA

Re: linearGradients

Postby tylerdurden » Sun Nov 24, 2013 11:07 am

Foolin around...

Spreadsheet generated 1px / color-bit (1-255)
SVG Image
https://dl.dropboxusercontent.com/s/x6n ... pasted.svg

Spreadsheet simply increments the color-bits and positions. e.g. : #ff0000 x=1, #ff0100 x=2, #ff0300 x=3...



Scaled in Inkscape:
SVG Image
https://dl.dropbox.com/s/jpte5g6bm0rb2o ... formed.svg

Regards,
TD
Have a nice day.

I'm using Inkscape 0.92.2 (5c3e80d, 2017-08-06), 64 bit win8.1

The Inkscape manual has lots of helpful info! http://tavmjong.free.fr/INKSCAPE/MANUAL/html/

Lazur
Posts: 4705
Joined: Tue Jun 14, 2016 10:38 am

Re: linearGradients

Postby Lazur » Sun Nov 24, 2013 11:46 am

JimJoyce wrote:...
viewBox="0 0 1300 250">
...


The time I realised that part, and what it does.
Those are not added to svg-s saved with inkscape.
An important part to be added if someone wants to embed an svg image into a frame.
http://forum.inkscapecommunity.com/index.php?topic=95.0


tylerdurden wrote:...
Spreadsheet simply increments the color-bits and positions. e.g. : #ff0000 x=1, #ff0100 x=2, #ff0300 x=3...



Scaled in Inkscape:
SVG Image...


The funny thing is that it is rendered smoother than that gradient with the same small steps through chrome.

JimJoyce
Posts: 37
Joined: Fri Jun 19, 2009 7:06 pm

Re: linearGradients

Postby JimJoyce » Mon Nov 25, 2013 3:36 am

Hi Lazur,
I read your comment about some browsers failing.
I admit I've made Opera my default browser for SVG.
But I have five browsers at the ready, Firefox my normal favourite, then Opera.
I rarely use Chrome, use IE only to check it does something, and Safari, because it's available.
I note you you use varieties of Linux. I'm afraid I've not ventured that far.

You say Firefox and Chrome produce nothing.
I'm very surprised.
All five of my browsers produce what to me seems similar renderings of both the spectrum strips.
Admittedly, the Yellow and Cyan look extra vivid in the upper strip, but that's in all browsers.
Both Yellow and Cyan look dull and blurred in the lower strip.

Thanks for your views.
JJ

JimJoyce
Posts: 37
Joined: Fri Jun 19, 2009 7:06 pm

Re: linearGradients

Postby JimJoyce » Mon Nov 25, 2013 3:46 am

Hi Tylerdurden,
I note your point on using spreadsheets to generate your code.
I often use it when creating similar lines of code.
Didn't get round to using it this time.
Probably because I normally use #xxx to define my colours, rather than rgb().
I'm not sure how Excel (or Calc) could cope with hex sequence 0123456789abcdef !
Maybe it can ? ?
Thanks, JJ

tylerdurden
Posts: 2322
Joined: Sun Apr 14, 2013 12:04 pm
Location: Michigan, USA

Re: linearGradients

Postby tylerdurden » Mon Nov 25, 2013 5:06 am

LibreOffice Calc includes function: DEC2HEX, the only small bugaboo is 0-16 convert to single hex characters that need leading zeros added.

Here's part* :
SVG Image
https://dl.dropboxusercontent.com/s/bri ... asted2.svg

*Seems the board software clips the width in the embedded image above, so it needs to be viewed in its own window, to get the full monty.


Regards,
TD
Have a nice day.

I'm using Inkscape 0.92.2 (5c3e80d, 2017-08-06), 64 bit win8.1

The Inkscape manual has lots of helpful info! http://tavmjong.free.fr/INKSCAPE/MANUAL/html/

Lazur
Posts: 4705
Joined: Tue Jun 14, 2016 10:38 am

Re: linearGradients

Postby Lazur » Mon Nov 25, 2013 8:08 am

Hi.

Currently running chrome, it renders "relatively" ok.
Image

Firefox doesn't render at all on this machine, and here is how the built-in viewer renders the svg:
Image

Lazur
Posts: 4705
Joined: Tue Jun 14, 2016 10:38 am

Re: linearGradients

Postby Lazur » Mon Nov 25, 2013 8:50 am

tylerdurden wrote:LibreOffice Calc includes function: DEC2HEX, the only small bugaboo is 0-16 convert to single hex characters that need leading zeros added.

Here's part* :
SVG Image
https://dl.dropboxusercontent.com/s/bri ... asted2.svg

*Seems the board software clips the width in the embedded image above, so it needs to be viewed in its own window, to get the full monty.


Regards,
TD



Maybe not as precise due to the lack of input options, but you can draw similar
with the tiled clone panel very fast. http://tavmjong.free.fr/INKSCAPE/MANUAL/html/Tiles-Color.html

Giving a rectangle an undefined fill, by making tiled clones of it with 1 rows and 334 columns,
and setting 0,3% for the hue value change percentage for each column will cover all the hue range.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 4 guests