Reshader

version shield
ci shield
license shield
stars shield

A micro, highly-customisable JavaScript library to get the shades of colors

Scroll down to see more

# Getting started

It's very simple to comence:

yarn add reshader or npm install reshader

Note: In case you are using RequireJS or want reshader on your window, please refer to the distribution file.

# Usage

You can simply call reshader passing a color in the first parameter.

It returns an object with two properties—you will learn more further—with the variations of the given color.

Use and abuse of the API to customise the output as much as you want or need.

Important to know

Reshader will always sort the colors it returns from the lightest shade to the darkest.


  import reshader from 'reshader'

  const { palette } = reshader('#6EEB83')
  
#FFFFFF
#FAFEFB
#D2F9D8
#ADF4B9
#8CEF9D
#6EEB83
#50E769
#35E352
#1FDD3E
#1CC738
#19B332

# API

reshader takes two parameters: color and options respectively.

# color

Type
string
Required

The color you want to extract the shades of. It's a string, but must satisfy one of these formats: hex, rgb or hsl.


  reshader('#EB6E6E')
  reshader('rgb(235, 110, 110)')
  reshader('hsl(0, 75.8%, 67.6%)')
  

palette

variations.all

variations.lighter

variations.darker

# options.numberOfVariations

Type
string
Default
5

The number of variations you want to extract out of the color you entered. This option will get both lighter and darker shades in the same amount, meaning that if you set it as 10, it will get you 10 lighter shades and 10 darker shades.


  const {
    palette,
    variations
  } = reshader('#A56EEB', { numberOfVariations: 10 })
  

palette

variations.all

variations.lighter

variations.darker

# options.contrastRatio

Type
number
Default
0.1

The contrast ratio of the shades. The lower, the smoother.

Important to know

You'll only reach black (#000000) when you set this property to 1.


  const {
    palette,
    variations
  } = reshader('#EBDE6E', { contrastRatio: 0.05 })
  

palette

variations.all

variations.lighter

variations.darker

# options.outputModel

Type
string
Default
hex

The color model reshader gonna output regardless of the color model of the input. That being said, if you entered a hex code, it'll output the model you set through this option. Please, use only one of the accepted output models.


  const {
    palette,
    variations
  } = reshader('#30B825', { outputModel: 'rgb' })
  

palette

variations.all

variations.lighter

variations.darker

# options.shouldIgnoreRepeated

Type
boolean
Default
false

When you get the shades of #FFFFFF, the lighter shades of it will all be #FFFFFF because, well, there's no lighter color than white. By setting this option to true, your output won't retrieve the repeated variations, except the #FFFFFF itself.


  const {
    palette,
    variations
  } = reshader('#FFFFFF', { shouldIgnoreRepeated: true })
  

palette

variations.all

variations.lighter

variations.darker

# options.shouldIgnoreWhites

Type
boolean
Default
false

Set this option to true when you don't want whites in your color palette or shade variations.


  const {
    palette,
    variations
  } = reshader('#6ED0EB', { shouldIgnoreWhites: true })
  

palette

variations.all

variations.lighter

variations.darker

# options.shouldIgnoreBlacks

Type
boolean
Default
false

Set this option to true when you don't want blacks in your color palette or shade variations.


  const {
    palette,
    variations
  } = reshader('#EBA26E', { shouldIgnoreBlacks: true, contrastRatio: 1 })
  

palette

variations.all

variations.lighter

variations.darker

An empty array

# Understanding palette and variations

reshader('#FF0000') returns an object containing the palette and variations properties. Let's take a look on how they work.

palette array

An array containing all the shades plus the color you passed in the first argument. That being said, reshader('#FF0000').palette would return an array with 11 indices. 5 lighter shades, 5 darker shades and #FF0000. It'll always be positioned right in the middle of the array—in our case, you'll be able to find it accessing the index 5.

variations object

  • .all array

    Returns only the variations of a given color, without the given color itself. For knowledge's sakes, The palette array contains 11 elements whilst variations will expose only 10 because it discards the given color. Basically, it's a merge of variations.lighter and variations.darker.

    Important to know

    There will be a jump between the colors in the middle of the this array because the given color will always be the most neutral shade and is unavailable here.

  • .lighter array

    Returns only the lighter shades of the given color, including whites. If the numberOfVariations is 10, this array will have 10 elements and it doesn't include the given color.

  • .darker array

    Returns only the darker shades of the given color, including blacks. If the numberOfVariations is 10, this array will have 10 elements and it doesn't include the given color.

To emphasize

Regardless of what array you're working with (palette, variations.all, etc.), Reshader will always sort them from the lightest shade to the darkest, being the value on the index 0 the lightest and the value on the last index the darkest.

# Accepted output models

The accepted models you can input are slightly different from the ones reshader is capable of output. It only accepts three entrance color models: hex, rgb and hsl, whilst it is capable of output hex (default), rgb, hsl, hsv, cmyk and ansi256. This is because it follows the API provided by its core, Qix's color.

# Motivation

It started when I needed a way to have consistent colors in my css-in-js apps. Unfortunately, picking colors by the eye was not getting me the best results, specially on the long term, and it was consuming unnecessary amounts of time to get the shades I wanted to. Prior to Reshader, I used to have a colors.js file and it used to look like this:


  // ./UI/colors.js

  export const colors = {
    gray: {
      0: '#FAFAFA',
      1: '#F2F2F2',
      2: '#DDDDDD'
    },

    red: {
      0: '#FFBBBB',
      1: '#FF8080',
      2: '#FF5050'
    },

    blue: {
      0: '#B6CBFF',
      1: '#8AABFF',
      2: '#326CFF'
    }
  }
  

  // MyComponent.js

  import React from 'react'
  import styled from 'styled-components'
  import { colors } from './UI'

  const Container = styled.div`
    background-color: ${colors.gray[0]};
  `

  const MyComponent = ({ children }) => <Container>{children}</Container>

  export default MyComponent
  

As you can see, all the colors were handpicked, manually mapped and prone to inconsistencies. But I wanted something programatically, consistent and simple. Then I came up with Reshader and my new colors.js looks just like this:


  import reshader from 'reshader'

  export const colors = {
    gray: reshader('#DDDDDD').palette,
    red: reshader('#FF5050').palette,
    blue: reshader('#326CFF').palette
  }
  

Boom! It's working with the same implementation as before, where gray[0] is the lightest shade and gray[gray.length - 1] is the darkest.

# Frequently Asked Questions

How are the mathematics behind the colors being done?

Please, refer to Qix-'s Color source file.

Why would I use Reshader?

Shallowly saying, if you want or need to generate shades of colors programatically.

Why would I use Reshader rather than ___?

Good question though the answer is very trivial: use your reality to respond yourself. Maybe it would be useful, maybe not. If you feel that another solution helps you better, go for it!

How can developers and designers work together with Reshader?

At first, Reshader was designed to help only developers, but after some feedbacks from different perspectives, it must be said that this answer is yet to come.

Isn't Reshader an overkill?

Maybe, yet it's very practical. From the distant outlook, it seems way too much to handle such a simple thing, but once you start using it, you're gonna feel the convenience. Not worring about shades but only the main colors of your app is beautiful and gonna boost your maintainance reability as well as your productivity.

Isn't performance an issue?

It depends. It may be an issue if you call reshader multiple times in runtime, but if you glue it to compilation time you're probably fine.

Why the website of this open source library is under a .com domain?

Because .com is cheaper than .io yet widely known and easy to remember. That's it.

Where should I submit my questions, feedbacks, issues, ideas, etc?

Make our repository yours.

# Author

A photo of Guilherme Oderdenge, the author of Reshader.
Guilherme "chiefGui" Oderdenge

A Brazilian JavaScript developer.

Also, a special thanks to Josh Junon, the author of the amazing Color—the library dealing with the magic Reshader uses to create the shades.