Top Related Projects
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Interactive visualizations of time series using JavaScript and the HTML canvas tag
Simple HTML5 Charts using the <canvas> tag
📈 A small, fast chart for time series, lines, areas, ohlc & bars
Open-source JavaScript charting library behind Plotly and Dash
Highcharts JS, the JavaScript charting framework
Quick Overview
Chroma.js is a small-footprint JavaScript library for color manipulation and conversion. It offers a wide range of color operations, including color scales, color space conversions, and color mixing, making it ideal for data visualization and design applications.
Pros
- Lightweight and efficient, with a small file size
- Supports various color spaces and formats (RGB, HSL, LAB, etc.)
- Provides advanced color manipulation functions like interpolation and blending
- Well-documented with clear examples and API reference
Cons
- Limited support for color accessibility features
- May have a steeper learning curve for beginners compared to simpler color libraries
- Lacks some advanced features found in larger color management systems
- Not actively maintained, with infrequent updates
Code Examples
Creating a color scale:
const scale = chroma.scale(['#fafa6e', '#2A4858'])
.mode('lch')
.colors(5);
console.log(scale); // Outputs an array of 5 color values
Manipulating color brightness:
const color = chroma('#FF0000');
const darkerColor = color.darken(2);
const brighterColor = color.brighten(1.5);
console.log(darkerColor.hex()); // Outputs a darker red
console.log(brighterColor.hex()); // Outputs a brighter red
Blending colors:
const color1 = chroma('#FF0000');
const color2 = chroma('#0000FF');
const blendedColor = chroma.blend(color1, color2, 'multiply');
console.log(blendedColor.hex()); // Outputs the blended color
Getting Started
To use Chroma.js in your project, you can include it via CDN or install it using npm:
<!-- Include via CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/2.4.2/chroma.min.js"></script>
Or install using npm:
npm install chroma-js
Then, in your JavaScript file:
import chroma from 'chroma-js';
// Create a color
const color = chroma('#FF0000');
// Manipulate the color
const lighterColor = color.brighten();
console.log(lighterColor.hex()); // Outputs the lighter color
Competitor Comparisons
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Pros of d3
- Comprehensive data visualization library with a wide range of chart types and advanced features
- Large and active community, extensive documentation, and numerous examples
- Powerful data binding and DOM manipulation capabilities
Cons of d3
- Steeper learning curve due to its extensive API and low-level approach
- Larger file size and potential performance overhead for simpler visualizations
- May be overkill for basic color manipulation tasks
Code Comparison
d3 (color manipulation):
const color = d3.rgb(255, 0, 0);
const darkerColor = color.darker(0.2);
const hslColor = color.hsl();
chroma.js:
const color = chroma(255, 0, 0);
const darkerColor = color.darken(0.2);
const hslColor = color.hsl();
Summary
While d3 is a powerful and versatile data visualization library, chroma.js is more focused on color manipulation and generation. d3 offers a wider range of features but comes with a steeper learning curve and larger file size. chroma.js provides a simpler API for color-related tasks and may be more suitable for projects primarily focused on color manipulation. The code comparison shows that both libraries offer similar color manipulation capabilities, with chroma.js having a slightly more concise syntax for these specific operations.
Interactive visualizations of time series using JavaScript and the HTML canvas tag
Pros of dygraphs
- Specialized in interactive time-series charting
- Supports large datasets with efficient rendering
- Extensive documentation and examples
Cons of dygraphs
- Limited to line and bar charts
- Steeper learning curve for complex customizations
- Less flexible for general-purpose color manipulation
Code Comparison
dygraphs (creating a simple line chart):
new Dygraph(document.getElementById("graphdiv"),
"Date,Temperature\n" +
"2008-05-07,75\n" +
"2008-05-08,70\n" +
"2008-05-09,80",
{ title: 'Temperature Over Time' }
);
chroma.js (creating a color scale):
var scale = chroma.scale(['#fafa6e','#2A4858'])
.mode('lch').colors(6);
Summary
dygraphs excels in creating interactive time-series charts with large datasets, while chroma.js focuses on color manipulation and generation. dygraphs offers more specialized charting capabilities but is limited to specific chart types. chroma.js provides greater flexibility for color-related tasks but doesn't handle charting directly. The choice between the two depends on whether the primary need is for time-series visualization (dygraphs) or advanced color manipulation (chroma.js).
Simple HTML5 Charts using the <canvas> tag
Pros of Chart.js
- Comprehensive charting library with various chart types (line, bar, pie, etc.)
- Responsive and interactive charts out of the box
- Extensive documentation and community support
Cons of Chart.js
- Larger file size and potentially heavier performance impact
- Less flexibility for custom color manipulation and generation
Code Comparison
Chart.js (creating a simple line chart):
const ctx = document.getElementById('myChart').getContext('2d');
new Chart(ctx, {
type: 'line',
data: {
labels: ['January', 'February', 'March'],
datasets: [{
label: 'Sales',
data: [12, 19, 3]
}]
}
});
Chroma.js (generating a color scale):
const scale = chroma.scale(['#fafa6e', '#2A4858'])
.mode('lch')
.colors(5);
While Chart.js is focused on creating various types of charts with built-in interactivity, Chroma.js specializes in color manipulation and generation. Chart.js is better suited for quickly creating standard charts, while Chroma.js excels at creating custom color scales and performing color-related operations. The choice between the two depends on the specific needs of your project.
📈 A small, fast chart for time series, lines, areas, ohlc & bars
Pros of uPlot
- Extremely fast and lightweight charting library, optimized for large datasets
- Supports time series data with high-performance rendering
- Minimal dependencies and small bundle size
Cons of uPlot
- Limited chart types compared to Chroma.js
- Steeper learning curve for customization
- Less focus on color manipulation and generation
Code Comparison
uPlot:
let uplot = new uPlot(opts, data, document.body);
Chroma.js:
let color = chroma.scale(['#fafa6e', '#2A4858']).mode('lch').colors(6);
Key Differences
uPlot is primarily a charting library focused on performance and efficiency for large datasets, particularly time series data. Chroma.js, on the other hand, is a color manipulation library that excels in creating and working with color scales, which can be useful for data visualization but serves a different primary purpose.
While uPlot provides powerful charting capabilities with minimal overhead, Chroma.js offers extensive color-related functions that can enhance the visual appeal of charts and other graphical elements. The choice between the two depends on the specific needs of the project: high-performance charting (uPlot) or advanced color manipulation (Chroma.js).
Open-source JavaScript charting library behind Plotly and Dash
Pros of Plotly.js
- Comprehensive data visualization library with a wide range of chart types
- Interactive and customizable plots with built-in animations and hover effects
- Supports both 2D and 3D visualizations
Cons of Plotly.js
- Larger file size and potentially slower load times compared to Chroma.js
- Steeper learning curve due to its extensive feature set
- May be overkill for simple color-related tasks
Code Comparison
Chroma.js (color manipulation):
const color = chroma('#FF0000');
const darkerColor = color.darken(0.5);
console.log(darkerColor.hex());
Plotly.js (basic plot):
const data = [{x: [1, 2, 3], y: [2, 4, 6], type: 'scatter'}];
const layout = {title: 'Simple Plot'};
Plotly.newPlot('myDiv', data, layout);
Summary
Plotly.js is a powerful data visualization library offering a wide range of chart types and interactive features. It's ideal for complex data representation but may be excessive for simple color-related tasks. Chroma.js, on the other hand, specializes in color manipulation and is more lightweight, making it a better choice for projects focused primarily on color operations.
Highcharts JS, the JavaScript charting framework
Pros of Highcharts
- Comprehensive charting library with a wide range of chart types and customization options
- Extensive documentation and active community support
- Built-in responsiveness and cross-browser compatibility
Cons of Highcharts
- Commercial license required for most use cases
- Steeper learning curve due to its extensive feature set
- Larger file size, which may impact page load times
Code Comparison
Highcharts (creating a basic line chart):
Highcharts.chart('container', {
series: [{
data: [1, 2, 3, 4, 5]
}]
});
Chroma.js (generating a color scale):
const scale = chroma.scale(['#fafa6e', '#2A4858']);
const color = scale(0.5).hex();
Key Differences
Highcharts is a full-featured charting library, while Chroma.js focuses specifically on color manipulation and generation. Highcharts is better suited for creating complex data visualizations, whereas Chroma.js excels in color-related tasks such as creating color scales, manipulating color spaces, and generating color palettes.
Chroma.js is lightweight and open-source, making it ideal for projects that primarily need color-related functionality. Highcharts, on the other hand, offers a comprehensive solution for data visualization but comes with licensing costs for commercial use.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Chroma.js
Chroma.js is a tiny small-ish zero-dependency JavaScript library for all kinds of color conversions and color scales.
Usage
Install from npm
npm install chroma-js
Import package into project
import chroma from "chroma-js";
Initiate and manipulate colors:
chroma('#D4F880').darken().hex(); // #a1c550
Working with color scales is easy, too:
scale = chroma.scale(['white', 'red']);
scale(0.5).hex(); // #FF7F7F
Lab/Lch interpolation looks better than RGB
chroma.scale(['white', 'red']).mode('lab');
Custom domains! Quantiles! Color Brewer!!
chroma.scale('RdYlBu').domain(myValues, 7, 'quantiles');
And why not use logarithmic color scales once in your life?
chroma.scale(['lightyellow', 'navy']).domain([1, 100000], 7, 'log');
Like it?
Why not dive into the interactive documentation (there's a static version, too). You can download chroma.min.js or use the hosted version on cdnjs.com.
You can use it in node.js, too!
npm install chroma-js
Or you can use it in SASS using chromatic-sass!
Want to contribute?
Come over and say hi in our Discord channel!
Build instructions
First clone the repository and install the dev dependencies:
git clone git@github.com:gka/chroma.js.git
cd chroma.js
npm install
Then compile the coffee-script source files to the build files:
npm run build
Don't forget to tests your changes! You will probably also want to add new test to the /test
folder in case you added a feature.
npm test
And to update the documentation just run
npm run docs
To preview the docs locally you can use
npm run docs-preview
Similar Libraries / Prior Art
- Chromatist
- GrapeFruit (Python)
- colors.py (Python)
- d3.js
- Color Art (Rust)
Author
Chroma.js is written by Gregor Aisch.
License
Released under BSD license. Versions prior to 0.4 were released under GPL.
Further reading
FAQ
There have been no commits in X weeks. Is chroma.js dead?
No! It's just that the author of this library has other things to do than devoting every week of his life to making cosmetic changes to a piece of software that is working just fine as it is, just so that people like you don't feel like it's abandoned and left alone in this world to die. Bugs will be fixed. Some new things will come at some point. Patience.
I want to help maintaining chroma.js!
Yay, that's awesome! Please say hi at our Discord chat to get in touch
Top Related Projects
Bring data to life with SVG, Canvas and HTML. :bar_chart::chart_with_upwards_trend::tada:
Interactive visualizations of time series using JavaScript and the HTML canvas tag
Simple HTML5 Charts using the <canvas> tag
📈 A small, fast chart for time series, lines, areas, ohlc & bars
Open-source JavaScript charting library behind Plotly and Dash
Highcharts JS, the JavaScript charting framework
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot