├── .circleci └── config.yml ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── README.md ├── index.js ├── package.json └── test └── test.js /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | - image: circleci/node:10 6 | steps: 7 | - checkout 8 | - restore_cache: 9 | key: dependency-cache-{{ checksum "package.json" }} 10 | - run: 11 | name: install-npm-wee 12 | command: npm install 13 | - save_cache: 14 | key: dependency-cache-{{ checksum "package.json" }} 15 | paths: 16 | - ./node_modules 17 | - run: 18 | name: test 19 | command: npm test 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 6 4 | - 10 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | 6 | ## [2.0.1](https://github.com/tmcw/relative-luminance/compare/v2.0.0...v2.0.1) (2018-12-30) 7 | 8 | 9 | 10 | 11 | # [2.0.0](https://github.com/tmcw/relative-luminance/compare/v1.0.2...v2.0.0) (2018-08-20) 12 | 13 | 14 | ### Features 15 | 16 | * UMD builds, simplified stack ([0bef765](https://github.com/tmcw/relative-luminance/commit/0bef765)) 17 | 18 | 19 | ### BREAKING CHANGES 20 | 21 | * completely new main, unpkg, and module entry points. 22 | 23 | 24 | 25 | 26 | ## [1.0.2](https://github.com/tmcw/relative-luminance/compare/v1.0.1...v1.0.2) (2018-04-16) 27 | 28 | 29 | ### Bug Fixes 30 | 31 | * move esm dependency out devDependencies ([6d6d5bf](https://github.com/tmcw/relative-luminance/commit/6d6d5bf)) 32 | 33 | 34 | 35 | 36 | ## [1.0.1](https://github.com/tmcw/relative-luminance/compare/v1.0.0...v1.0.1) (2018-03-21) 37 | 38 | 39 | ### Bug Fixes 40 | 41 | * Be conservative about files included in the package. ([f96e583](https://github.com/tmcw/relative-luminance/commit/f96e583)) 42 | 43 | 44 | 45 | 46 | # [1.0.0](https://github.com/tmcw/relative-luminance/compare/v0.0.2...v1.0.0) (2018-03-21) 47 | 48 | 49 | ### Features 50 | 51 | * JS modules: use esm to provide both CommonJS and ES6 entry points ([2984a5f](https://github.com/tmcw/relative-luminance/commit/2984a5f)) 52 | 53 | 54 | 55 | 56 | ## [0.0.2](https://github.com/tmcw/relative-luminance/compare/v0.0.1...v0.0.2) (2017-05-03) 57 | 58 | 59 | 60 | 61 | ## 0.0.1 (2017-05-03) 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Greenkeeper badge](https://badges.greenkeeper.io/tmcw/relative-luminance.svg)](https://greenkeeper.io/) 2 | [![CircleCI](https://circleci.com/gh/tmcw/relative-luminance/tree/master.svg?style=svg)](https://circleci.com/gh/tmcw/relative-luminance/tree/master) 3 | 4 | ## relative-luminance 5 | 6 | npm install relative-luminance 7 | 8 | Calculate the [relative luminance](http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef) of 9 | an RGB triplet color. 10 | 11 | Note that as of 1.0.0, this module doesn't directly export a function if you're using 12 | CommonJS: use the `default` export, like: 13 | 14 | ```js 15 | var luminance = require('relative-luminance').default; 16 | ``` 17 | 18 | ### API 19 | 20 | 21 | 22 | ##### Table of Contents 23 | 24 | - [relativeLuminance](#relativeluminance) 25 | - [Parameters](#parameters) 26 | - [Examples](#examples) 27 | 28 | #### relativeLuminance 29 | 30 | Given a 3-element array of R, G, B varying from 0 to 255, return the luminance 31 | as a number from 0 to 1. 32 | 33 | ##### Parameters 34 | 35 | - `rgb` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>** 3-element array of a color 36 | 37 | ##### Examples 38 | 39 | ```javascript 40 | var luminance = require('relative-luminance'); 41 | var black_lum = luminance([0, 0, 0]); // 0 42 | ``` 43 | 44 | Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** luminance, between 0 and 1 45 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // # Relative luminance 2 | // 3 | // http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef 4 | // https://en.wikipedia.org/wiki/Luminance_(relative) 5 | // https://en.wikipedia.org/wiki/Luminosity_function 6 | // https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients 7 | 8 | // red, green, and blue coefficients 9 | const rc = 0.2126; 10 | const gc = 0.7152; 11 | const bc = 0.0722; 12 | // low-gamma adjust coefficient 13 | const lowc = 1 / 12.92; 14 | 15 | function adjustGamma(_) { 16 | return Math.pow((_ + 0.055) / 1.055, 2.4); 17 | } 18 | 19 | /** 20 | * Given a 3-element array of R, G, B varying from 0 to 255, return the luminance 21 | * as a number from 0 to 1. 22 | * @param {Array} rgb 3-element array of a color 23 | * @returns {number} luminance, between 0 and 1 24 | * @example 25 | * var luminance = require('relative-luminance'); 26 | * var black_lum = luminance([0, 0, 0]); // 0 27 | */ 28 | export default function relativeLuminance(rgb) { 29 | const rsrgb = rgb[0] / 255; 30 | const gsrgb = rgb[1] / 255; 31 | const bsrgb = rgb[2] / 255; 32 | 33 | const r = rsrgb <= 0.03928 ? rsrgb * lowc : adjustGamma(rsrgb); 34 | const g = gsrgb <= 0.03928 ? gsrgb * lowc : adjustGamma(gsrgb); 35 | const b = bsrgb <= 0.03928 ? bsrgb * lowc : adjustGamma(bsrgb); 36 | 37 | return r * rc + g * gc + b * bc; 38 | } 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "relative-luminance", 3 | "version": "2.0.1", 4 | "description": "calculate relative luminance", 5 | "source": "index.js", 6 | "main": "dist/index.js", 7 | "module": "dist/index.m.js", 8 | "umd:main": "dist/index.umd.js", 9 | "unpkg": "dist/index.umd.js", 10 | "directories": { 11 | "test": "test" 12 | }, 13 | "scripts": { 14 | "release": "microbundle && standard-version", 15 | "test": "microbundle && tape test/*.js", 16 | "doc": "documentation readme -s API index.js" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/tmcw/relative-luminance.git" 21 | }, 22 | "keywords": [ 23 | "color", 24 | "luminance" 25 | ], 26 | "files": [ 27 | "dist" 28 | ], 29 | "author": "Tom MacWright", 30 | "license": "BSD-2-Clause", 31 | "devDependencies": { 32 | "documentation": "^11.0.0", 33 | "microbundle": "^0.11.0", 34 | "standard-version": "^6.0.1", 35 | "tape": "^4.9.1" 36 | }, 37 | "dependencies": { 38 | "esm": "^3.0.84" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const test = require("tape"); 2 | const luminance = require("../"); 3 | 4 | test("luminance", t => { 5 | t.equal(luminance([0, 0, 0]), 0, "black"); 6 | t.equal(luminance([255, 255, 255]), 1, "white"); 7 | t.end(); 8 | }); 9 | --------------------------------------------------------------------------------