# Cross-browser imageless linear gradients v2

DevFeed: [Cross-browser imageless linear gradients v2](<https://devfeed.tech/articles/cross-browser-imageless-linear-gradients-v2-51951.md>)

Original publisher: [Read original article](<https://lea.verou.me/2009/04/cross-browser-imageless-linear-gradients-v2/>)

Author: Lea Verou

Published: 2009-04-24T00:00:00Z

Content type: release

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [browser](<https://devfeed.tech/topics/browser.md>), [Image](<https://devfeed.tech/topics/image.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [array](<https://devfeed.tech/tags/array.md>), [blog](<https://devfeed.tech/tags/blog.md>), [browser](<https://devfeed.tech/tags/browser.md>), [browsers](<https://devfeed.tech/tags/browsers.md>), [bug](<https://devfeed.tech/tags/bug.md>), [color](<https://devfeed.tech/tags/color.md>), [cross](<https://devfeed.tech/tags/cross.md>), [gradient](<https://devfeed.tech/tags/gradient.md>), [image](<https://devfeed.tech/tags/image.md>), [js](<https://devfeed.tech/tags/js.md>), [linear](<https://devfeed.tech/tags/linear.md>), [make](<https://devfeed.tech/tags/make.md>), [more](<https://devfeed.tech/tags/more.md>), [null](<https://devfeed.tech/tags/null.md>), [original](<https://devfeed.tech/tags/original.md>), [paint](<https://devfeed.tech/tags/paint.md>), [photoshop](<https://devfeed.tech/tags/photoshop.md>), [releases](<https://devfeed.tech/tags/releases.md>), [script](<https://devfeed.tech/tags/script.md>), [transparent](<https://devfeed.tech/tags/transparent.md>), [v2](<https://devfeed.tech/tags/v2.md>), [vertical](<https://devfeed.tech/tags/vertical.md>)

## AI overview

The updated script creates imageless linear gradients with more than two colors, which the author needed to represent Hue in a color picker, and supports a fully transparent gradient color for its 2D plane. Colors are now passed as an array to the `Gradient` constructor and `g.paint()`, which takes the color array (or `null`) and a vertical/horizontal direction. The new `g.setColorAt(index, color)` changes an individual color, while `g.direction()` sets or toggles direction. `g.startColor` and `g.endColor` are replaced by `g.colors`. Version 2.0.1 fixes a `transparent`-color bug in non-IE browsers for multicolor gradients when the transparent color was not first or last.

## Source excerpt

A while ago, I posted a script of mine for creating 2-color cross-browser imageless linear gradients. As I stated there, I needed them for a color picker I have to create. And even though 2-color gradients are sufficient for most components, in most color spaces, I had forgotten an important one: Hue. You can't represent Hue with a 2-color gradient! So, I had to revise the script, and make it able to produce linear gradients of more than 2 colors. Furthermore, I needed to be able to specify a fully transparent color as one of the gradient colors, in order to create the photoshop-like 2d plane used by the picker (and no, a static image background like the one used in most JS color pickers wouldn't suffice, for reasons irrelevant with this post). I hereby present you Cross-browser, imageless, linear gradients v2! The API has stayed just the same, with the following differences: You may specify the keyword "transparent" instead of a #RRGGBB color (that was such a pain to implement btw!). When creating a Gradient object, color strings are now defined in an array. Example: var g = new Gradient(200, 100, ['#000000', '#ff1166', '#23ff46'], true); When calling g.paint() it now takes 2 arguments instead of 3: The new color array (or null if you don't want that to change) and the direction (true for vertical, false for horizontal). For example: g.paint(['#000000', '#ff1166', '#23ff46'], true); 2 new methods have been added: g.setColorAt(index, color) and g.direction(newDirection). The first allows you to set a particular gradient color (index starting from 0) and the second to alter or toggle the direction (if you specify a direction parameter, you set the direction, if you call it with no parameters, it toggles from horizontal to vertical). The fields g.startColor and g.endColor have been replaced by the array g.colors. Update: v2.0.1 Fixed a small bug with the 'transparent' keyword that affected multi-color gradients in browsers != IE when the transparent color wasn't first