• Stars
    star
    194
  • Rank 200,219 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast and small gradients manipulation, built on top of TinyColor

tinygradient

npm version jsDelivr CDN GZIP size Build Status

Easily generate color gradients with an unlimited number of color stops and steps.

Live demo

Installation

$ npm install tinygradient

Dependencies

Usage

The gradient can be generated using RGB or HSV interpolation. HSV usually produces brighter colors.

Initialize gradient

The tinygradient constructor takes a list or an array of colors stops.

// using varargs
var gradient = tinygradient('red', 'green', 'blue');

// using array
var gradient = tinygradient([
  '#ff0000',
  '#00ff00',
  '#0000ff'
]);

The colors are parsed with TinyColor, multiple formats are accepted.

var gradient = tinygradient([
  tinycolor('#ff0000'),       // tinycolor object
  {r: 0, g: 255, b: 0},       // RGB object
  {h: 240, s: 1, v: 1, a: 1}, // HSVa object
  'rgb(120, 120, 0)',         // RGB CSS string
  'gold'                      // named color
]);

You can also specify the position of each color stop (between 0 and 1). If no position is specified, stops are distributed equidistantly.

var gradient = tinygradient([
  {color: '#d8e0de', pos: 0},
  {color: '#255B53', pos: 0.8},
  {color: '#000000', pos: 1}
]);

Generate gradient

Each method takes at least the number of desired steps.

The generated gradients might have one more step in certain conditions.

// RGB interpolation
var colorsRgb = gradient.rgb(9);

rgb

// HSV clockwise interpolation
var colorsHsv = gradient.hsv(9);

hsv

// HSV counter-clockwise interpolation
var colorsHsv = gradient.hsv(9, true);

hsv2

There are also two methods which will automatically choose between clockwise and counter-clockwise.

// HSV interpolation using shortest arc between colors
var colorsHsv = gradient.hsv(9, 'short');

// HSV interpolation using longest arc between colors
var colorsHsv = gradient.hsv(9, 'long');

Each method returns an array of TinyColor objects, see available methods.

Get CSS gradient string

The css method will output a valid W3C string (without vendors prefix) to use with background-image CSS property.

// linear gradient to right (default)
var gradientStr = gradient.css();

// radial gradient ellipse at top left
var gradientStr = gradient.css('radial', 'farthest-corner ellipse at top left');

Get color at a specific position

Returns a single TinyColor object from a defined position in the gradient (from 0 to 1).

// with RGB interpolation
colorAt55Percent = gradient.rgbAt(0.55);

// with HSV interpolation
colorAt55Percent = gradient.hsvAt(0.55);

Reversing gradient

Returns a new instance of TinyGradient with reversed colors.

var reversedGradient = gradient.reverse();

Loop the gradient

Returns a new instance of TinyGradient with looped colors.

var loopedGradient = gradient.loop();

Position-only stops

I is possible to define stops with the pos property only and no color. This allows to define the position of the mid-point between the previous and the next stop.

var gradient = tinygradient([
  {color: 'black', pos: 0},
  {pos: 0.8}, // #808080 will be at 80% instead of 50%
  {color: 'white', pos: 1}
]);

License

This library is available under the MIT license.

More Repositories

1

jQuery-QueryBuilder

jQuery plugin offering an interface to create complex queries
JavaScript
1,680
star
2

RandomColor.php

A color generator for PHP
PHP
235
star
3

jekyll-bootstrap-doc

Simple documentation template for Github pages
HTML
89
star
4

angular-jqcloud

AngularJS directive for jQCloud 2 plugin
35
star
5

HyperCube

Dart
22
star
6

jQuery.extendext

jQuery.extend with configurable behaviour for arrays
JavaScript
21
star
7

squareicon

JavaScript library for generating abstract images from unique identifiers.
JavaScript
17
star
8

three.js-examples

A Bower package only containing the JavaScript files of the Three.js examples.
JavaScript
16
star
9

Rainmeter-Collection

Some Rainmeter skins I did when I was bored
NASL
12
star
10

angular-smilies

AngularJS filter & directive for smilies parsing
JavaScript
11
star
11

Rainmeter-TopProcesses

A Rainmeter plugin to show n-number of top processes by either memory or CPU consumption.
C#
10
star
12

UMWP-Autochanger

UMWP Autochanger is a little tool for multi-monitors users. It randomly and periodically changes the wallpaper of all your monitors and manages sets of wallpapers, allowing you to quickly change which images are used.
C++
8
star
13

Nixie-Clock-IN12

C++
4
star
14

Rainmeter-NowPlayingPlex

A Rainmeter plugin to read the currently playing track on Plex.
C#
4
star
15

Piwigo-PhotoSphere

Add a Photo Sphere viewer to Piwigo
JavaScript
4
star
16

CircleLamp

C++
3
star
17

Rainmeter-Tron-MCP

Full featured Rainmeter monitoring suite inspired by Tron movies.
HTML
2
star
18

Github-Network

A custom clone of the Github network view
JavaScript
2
star
19

hain-plugin-rss

RSS feed reader for Hain launcher
JavaScript
1
star
20

Rainmeter-NowPlayingJellyfin

A Rainmeter plugin to read the currently playing track on Jellyfin.
C#
1
star
21

grunt-qunit-blanket-lcov

Save LCOV reports generated by Blanket for Grunt QUnit.
JavaScript
1
star
22

rpi-led-matrix-server

A simple C++ program to display a slideshow of images on a HUB75 matrix display.
C++
1
star
23

PikaExplorer

A Pokemon Red/Blue/Yellow save game parser writen in JavaScript.
CSS
1
star