🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@react-style-system/core

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-style-system/core - npm Package Compare versions

Comparing version
0.1.1
to
0.2.0
+3
-73
index.esm.js
import React, { createContext, useMemo, useContext } from 'react';
import parseToRgba from '@ricokahler/parse-to-rgba';
import { readableColor, getContrast } from 'color2k';
export * from 'color2k';

@@ -20,73 +21,2 @@ var ColorContext = createContext(null);

// https://github.com/styled-components/polished/blob/0764c982551b487469043acb56281b0358b3107b/src/color/getLuminance.js
/**
* Returns a number (float) representing the luminance of a color.
*
* @example
* // Styles as object usage
* const styles = {
* background: getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff',
* background: getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?
* 'rgba(58, 133, 255, 1)' :
* 'rgba(255, 57, 149, 1)',
* }
*
* // styled-components usage
* const div = styled.div`
* background: ${getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff'};
* background: ${getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?
* 'rgba(58, 133, 255, 1)' :
* 'rgba(255, 57, 149, 1)'};
*
* // CSS in JS Output
*
* div {
* background: "#CCCD64";
* background: "rgba(58, 133, 255, 1)";
* }
*/
function getLuminance(color) {
if (color === 'transparent') return 0;
function f(x) {
const channel = x / 255;
return channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
}
const parsed = parseToRgba(color);
if (!parsed) {
throw new Error(`Could not parse color "${color}"`);
}
const [r, g, b] = parsed;
return parseFloat((0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)).toFixed(3));
}
// taken from:
/**
* Returns black or white for best contrast depending on the luminosity of the given color.
*/
function readableColor(color) {
return getLuminance(color) > 0.179 ? '#000' : '#fff';
}
// taken from:
/**
* Returns the contrast ratio between two colors based on
* [W3's recommended equation for calculating contrast](http://www.w3.org/TR/WCAG20/#contrast-ratiodef).
*
* @example
* const contrastRatio = getContrast('#444', '#fff');
*/
function getContrast(color1, color2) {
const luminance1 = getLuminance(color1);
const luminance2 = getLuminance(color2);
return parseFloat((luminance1 > luminance2 ? (luminance1 + 0.05) / (luminance2 + 0.05) : (luminance2 + 0.05) / (luminance1 + 0.05)).toFixed(2));
}
const hasBadDecorativeContrast = (a, b) => getContrast(a, b) < 1.5;

@@ -159,3 +89,3 @@

export { ColorContext, ColorContextProvider, ThemeContext, ThemeProvider, createReadablePalette, css, getContrast, getLuminance, readableColor, useColorContext, useTheme };
export { ColorContext, ColorContextProvider, ThemeContext, ThemeProvider, createReadablePalette, css, useColorContext, useTheme };
//# sourceMappingURL=index.esm.js.map
+1
-1

@@ -1,1 +0,1 @@

{"version":3,"file":"index.esm.js","sources":["../../packages/core/src/ColorContext.ts","../../packages/core/src/ColorContextProvider.tsx","../../packages/core/src/getLuminance.ts","../../packages/core/src/readableColor.ts","../../packages/core/src/getContrast.ts","../../packages/core/src/createReadablePalette.ts","../../packages/core/src/ThemeContext.ts","../../packages/core/src/ThemeProvider.tsx","../../packages/core/src/useColorContext.ts","../../packages/core/src/useTheme.ts","../../packages/core/src/css.ts"],"sourcesContent":["import { createContext } from 'react';\nimport { ColorContextValue } from './types';\n\nexport default createContext<ColorContextValue | null>(null);\n","import React, { useMemo } from 'react';\nimport ColorContext from './ColorContext';\n\ninterface Props {\n color: string;\n surface: string;\n children: React.ReactNode;\n}\n\nfunction ColorContextProvider({ color, surface, children }: Props) {\n const contextValue = useMemo(() => ({ color, surface }), [color, surface]);\n return (\n <ColorContext.Provider value={contextValue}>\n {children}\n </ColorContext.Provider>\n );\n}\n\nexport default ColorContextProvider;\n","import parseToRgba from '@ricokahler/parse-to-rgba';\n// taken from:\n// https://github.com/styled-components/polished/blob/0764c982551b487469043acb56281b0358b3107b/src/color/getLuminance.js\n\n/**\n * Returns a number (float) representing the luminance of a color.\n *\n * @example\n * // Styles as object usage\n * const styles = {\n * background: getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff',\n * background: getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?\n * 'rgba(58, 133, 255, 1)' :\n * 'rgba(255, 57, 149, 1)',\n * }\n *\n * // styled-components usage\n * const div = styled.div`\n * background: ${getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff'};\n * background: ${getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?\n * 'rgba(58, 133, 255, 1)' :\n * 'rgba(255, 57, 149, 1)'};\n *\n * // CSS in JS Output\n *\n * div {\n * background: \"#CCCD64\";\n * background: \"rgba(58, 133, 255, 1)\";\n * }\n */\nexport default function getLuminance(color: string): number {\n if (color === 'transparent') return 0;\n\n function f(x: number) {\n const channel = x / 255;\n return channel <= 0.03928\n ? channel / 12.92\n : ((channel + 0.055) / 1.055) ** 2.4;\n }\n\n const parsed = parseToRgba(color);\n\n if (!parsed) {\n throw new Error(`Could not parse color \"${color}\"`);\n }\n\n const [r, g, b] = parsed;\n return parseFloat((0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)).toFixed(3));\n}\n","// taken from:\n// https://github.com/styled-components/polished/blob/6f72ff07eed0bb63ada17369fde4986c9389e5f9/src/color/readableColor.js\nimport getLuminance from './getLuminance';\n\n/**\n * Returns black or white for best contrast depending on the luminosity of the given color.\n */\nexport default function readableColor(color: string): string {\n return getLuminance(color) > 0.179 ? '#000' : '#fff';\n}\n","// taken from:\n// https://github.com/styled-components/polished/blob/0764c982551b487469043acb56281b0358b3107b/src/color/getContrast.js\nimport getLuminance from './getLuminance';\n\n/**\n * Returns the contrast ratio between two colors based on\n * [W3's recommended equation for calculating contrast](http://www.w3.org/TR/WCAG20/#contrast-ratiodef).\n *\n * @example\n * const contrastRatio = getContrast('#444', '#fff');\n */\nexport default function getContrast(color1: string, color2: string): number {\n const luminance1 = getLuminance(color1);\n const luminance2 = getLuminance(color2);\n return parseFloat(\n (luminance1 > luminance2\n ? (luminance1 + 0.05) / (luminance2 + 0.05)\n : (luminance2 + 0.05) / (luminance1 + 0.05)\n ).toFixed(2),\n );\n}\n","import readableColor from './readableColor';\nimport getContrast from './getContrast';\n\nimport { ReadableColorPalette } from './types';\n\nconst hasBadDecorativeContrast = (a: string, b: string) =>\n getContrast(a, b) < 1.5;\nconst hasBadReadableContrast = (a: string, b: string) => getContrast(a, b) < 3;\nconst hasBadAaContrast = (a: string, b: string) => getContrast(a, b) < 4.5;\nconst hasBadAaaContrast = (a: string, b: string) => getContrast(a, b) < 7;\n\nfunction createReadablePalette(\n color: string,\n surface = '#fff',\n): ReadableColorPalette {\n const readableContrast = readableColor(surface);\n\n return {\n original: color,\n decorative: hasBadDecorativeContrast(color, surface)\n ? readableContrast\n : color,\n readable: hasBadReadableContrast(color, surface) ? readableContrast : color,\n aa: hasBadAaContrast(color, surface) ? readableContrast : color,\n aaa: hasBadAaaContrast(color, surface) ? readableContrast : color,\n };\n}\n\nexport default createReadablePalette;\n","import { createContext } from 'react';\nexport default createContext<unknown>(null);\n","import React from 'react';\nimport ThemeContext from './ThemeContext';\n\ninterface Props {\n theme: unknown;\n children: React.ReactNode;\n}\n\nfunction ThemeProvider({ theme, children }: Props) {\n return (\n <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>\n );\n}\n\nexport default ThemeProvider;\n","import { useContext, useMemo } from 'react';\nimport ColorContext from './ColorContext';\nimport createReadablePalette from './createReadablePalette';\n\ninterface Props {\n color?: string;\n surface?: string;\n}\n\nfunction useColorContext(props?: Props) {\n const colorContext = useContext(ColorContext);\n\n if (!colorContext) {\n throw new Error(\n 'Could not find color context. Ensure this component is wrapped in a ColorContextProvider.',\n );\n }\n\n const color = props?.color || colorContext.color;\n const surface = props?.surface || colorContext.surface;\n\n return useMemo(\n () => ({\n color: createReadablePalette(color, surface),\n surface,\n }),\n [color, surface],\n );\n}\n\nexport default useColorContext;\n","import { useContext } from 'react';\nimport ThemeContext from './ThemeContext';\n\nfunction useTheme<T>(): T {\n const theme = useContext(ThemeContext) as T | null;\n\n if (!theme) {\n throw new Error(\n 'Could not find theme. Ensure this component is wrapped in a ThemeProvider',\n );\n }\n\n return theme;\n}\n\nexport default useTheme;\n","function css(strings: TemplateStringsArray, ...values: Array<string | number>) {\n let combined = '';\n\n for (let i = 0; i < strings.length; i += 1) {\n const currentString = strings[i];\n const currentValue = values[i] || '';\n combined += currentString + currentValue;\n }\n\n return combined;\n}\n\nexport default css;\n"],"names":["createContext","ColorContextProvider","color","surface","children","contextValue","useMemo","getLuminance","f","x","channel","parsed","parseToRgba","Error","r","g","b","parseFloat","toFixed","readableColor","getContrast","color1","color2","luminance1","luminance2","hasBadDecorativeContrast","a","hasBadReadableContrast","hasBadAaContrast","hasBadAaaContrast","createReadablePalette","readableContrast","original","decorative","readable","aa","aaa","ThemeProvider","theme","useColorContext","props","colorContext","useContext","ColorContext","useTheme","ThemeContext","css","strings","values","combined","i","length","currentString","currentValue"],"mappings":";;;AAGA,mBAAeA,aAAa,CAA2B,IAA3B,CAA5B;;ACMA,SAASC,oBAAT,CAA8B;AAAEC,EAAAA,KAAF;AAASC,EAAAA,OAAT;AAAkBC,EAAAA;AAAlB,CAA9B,EAAmE;AACjE,QAAMC,YAAY,GAAGC,OAAO,CAAC,OAAO;AAAEJ,IAAAA,KAAF;AAASC,IAAAA;AAAT,GAAP,CAAD,EAA6B,CAACD,KAAD,EAAQC,OAAR,CAA7B,CAA5B;AACA,sBACE,oBAAC,YAAD,CAAc,QAAd;AAAuB,IAAA,KAAK,EAAEE;AAA9B,KACGD,QADH,CADF;AAKD;;ACdD;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0Be,SAASG,YAAT,CAAsBL,KAAtB,EAA6C;AAC1D,MAAIA,KA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ources":["../../packages/core/src/ColorContext.ts","../../packages/core/src/ColorContextProvider.tsx","../../packages/core/src/createReadablePalette.ts","../../packages/core/src/ThemeContext.ts","../../packages/core/src/ThemeProvider.tsx","../../packages/core/src/useColorContext.ts","../../packages/core/src/useTheme.ts","../../packages/core/src/css.ts"],"sourcesContent":["import { createContext } from 'react';\nimport { ColorContextValue } from './types';\n\nexport default createContext<ColorContextValue | null>(null);\n","import React, { useMemo } from 'react';\nimport ColorContext from './ColorContext';\n\ninterface Props {\n color: string;\n surface: string;\n children: React.ReactNode;\n}\n\nfunction ColorContextProvider({ color, surface, children }: Props) {\n const contextValue = useMemo(() => ({ color, surface }), [color, surface]);\n return (\n <ColorContext.Provider value={contextValue}>\n {children}\n </ColorContext.Provider>\n );\n}\n\nexport default ColorContextProvider;\n","import { readableColor, getContrast } from 'color2k';\nimport { ReadableColorPalette } from './types';\n\nconst hasBadDecorativeContrast = (a: string, b: string) =>\n getContrast(a, b) < 1.5;\nconst hasBadReadableContrast = (a: string, b: string) => getContrast(a, b) < 3;\nconst hasBadAaContrast = (a: string, b: string) => getContrast(a, b) < 4.5;\nconst hasBadAaaContrast = (a: string, b: string) => getContrast(a, b) < 7;\n\nfunction createReadablePalette(\n color: string,\n surface = '#fff',\n): ReadableColorPalette {\n const readableContrast = readableColor(surface);\n\n return {\n original: color,\n decorative: hasBadDecorativeContrast(color, surface)\n ? readableContrast\n : color,\n readable: hasBadReadableContrast(color, surface) ? readableContrast : color,\n aa: hasBadAaContrast(color, surface) ? readableContrast : color,\n aaa: hasBadAaaContrast(color, surface) ? readableContrast : color,\n };\n}\n\nexport default createReadablePalette;\n","import { createContext } from 'react';\nexport default createContext<unknown>(null);\n","import React from 'react';\nimport ThemeContext from './ThemeContext';\n\ninterface Props {\n theme: unknown;\n children: React.ReactNode;\n}\n\nfunction ThemeProvider({ theme, children }: Props) {\n return (\n <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>\n );\n}\n\nexport default ThemeProvider;\n","import { useContext, useMemo } from 'react';\nimport ColorContext from './ColorContext';\nimport createReadablePalette from './createReadablePalette';\n\ninterface Props {\n color?: string;\n surface?: string;\n}\n\nfunction useColorContext(props?: Props) {\n const colorContext = useContext(ColorContext);\n\n if (!colorContext) {\n throw new Error(\n 'Could not find color context. Ensure this component is wrapped in a ColorContextProvider.',\n );\n }\n\n const color = props?.color || colorContext.color;\n const surface = props?.surface || colorContext.surface;\n\n return useMemo(\n () => ({\n color: createReadablePalette(color, surface),\n surface,\n }),\n [color, surface],\n );\n}\n\nexport default useColorContext;\n","import { useContext } from 'react';\nimport ThemeContext from './ThemeContext';\n\nfunction useTheme<T>(): T {\n const theme = useContext(ThemeContext) as T | null;\n\n if (!theme) {\n throw new Error(\n 'Could not find theme. Ensure this component is wrapped in a ThemeProvider',\n );\n }\n\n return theme;\n}\n\nexport default useTheme;\n","function css(strings: TemplateStringsArray, ...values: Array<string | number>) {\n let combined = '';\n\n for (let i = 0; i < strings.length; i += 1) {\n const currentString = strings[i];\n const currentValue = values[i] || '';\n combined += currentString + currentValue;\n }\n\n return combined;\n}\n\nexport default css;\n"],"names":["createContext","ColorContextProvider","color","surface","children","contextValue","useMemo","hasBadDecorativeContrast","a","b","getContrast","hasBadReadableContrast","hasBadAaContrast","hasBadAaaContrast","createReadablePalette","readableContrast","readableColor","original","decorative","readable","aa","aaa","ThemeProvider","theme","useColorContext","props","colorContext","useContext","ColorContext","Error","useTheme","ThemeContext","css","strings","values","combined","i","length","currentString","currentValue"],"mappings":";;;;AAGA,mBAAeA,aAAa,CAA2B,IAA3B,CAA5B;;ACMA,SAASC,oBAAT,CAA8B;AAAEC,EAAAA,KAAF;AAASC,EAAAA,OAAT;AAAkBC,EAAAA;AAAlB,CAA9B,EAAmE;AACjE,QAAMC,YAAY,GAAGC,OAAO,CAAC,OAAO;AAAEJ,IAAAA,KAAF;AAASC,IAAAA;AAAT,GAAP,CAAD,EAA6B,CAACD,KAAD,EAAQC,OAAR,CAA7B,CAA5B;AACA,sBAC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A,QAAQ,IAAIG,aAAa,GAAGC,YAA5B;AACD;;AAED,SAAOJ,QAAP;AACD;;;;"}
+17
-144
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('@ricokahler/parse-to-rgba')) :
typeof define === 'function' && define.amd ? define(['exports', 'react', '@ricokahler/parse-to-rgba'], factory) :
(global = global || self, factory(global.ReactStyleSystem = {}, global.React, global.parseToRgba));
}(this, (function (exports, React, parseToRgba) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('color2k')) :
typeof define === 'function' && define.amd ? define(['exports', 'react', 'color2k'], factory) :
(global = global || self, factory(global.ReactStyleSystem = {}, global.React, global.color2k));
}(this, (function (exports, React, color2k) { 'use strict';
var React__default = 'default' in React ? React['default'] : React;
parseToRgba = parseToRgba && Object.prototype.hasOwnProperty.call(parseToRgba, 'default') ? parseToRgba['default'] : parseToRgba;

@@ -27,147 +26,16 @@ var ColorContext = React.createContext(null);

function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
// https://github.com/styled-components/polished/blob/0764c982551b487469043acb56281b0358b3107b/src/color/getLuminance.js
/**
* Returns a number (float) representing the luminance of a color.
*
* @example
* // Styles as object usage
* const styles = {
* background: getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff',
* background: getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?
* 'rgba(58, 133, 255, 1)' :
* 'rgba(255, 57, 149, 1)',
* }
*
* // styled-components usage
* const div = styled.div`
* background: ${getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff'};
* background: ${getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?
* 'rgba(58, 133, 255, 1)' :
* 'rgba(255, 57, 149, 1)'};
*
* // CSS in JS Output
*
* div {
* background: "#CCCD64";
* background: "rgba(58, 133, 255, 1)";
* }
*/
function getLuminance(color) {
if (color === 'transparent') return 0;
function f(x) {
var channel = x / 255;
return channel <= 0.03928 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4);
}
var parsed = parseToRgba(color);
if (!parsed) {
throw new Error("Could not parse color \"".concat(color, "\""));
}
var _parsed = _slicedToArray(parsed, 3),
r = _parsed[0],
g = _parsed[1],
b = _parsed[2];
return parseFloat((0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)).toFixed(3));
}
// taken from:
/**
* Returns black or white for best contrast depending on the luminosity of the given color.
*/
function readableColor(color) {
return getLuminance(color) > 0.179 ? '#000' : '#fff';
}
// taken from:
/**
* Returns the contrast ratio between two colors based on
* [W3's recommended equation for calculating contrast](http://www.w3.org/TR/WCAG20/#contrast-ratiodef).
*
* @example
* const contrastRatio = getContrast('#444', '#fff');
*/
function getContrast(color1, color2) {
var luminance1 = getLuminance(color1);
var luminance2 = getLuminance(color2);
return parseFloat((luminance1 > luminance2 ? (luminance1 + 0.05) / (luminance2 + 0.05) : (luminance2 + 0.05) / (luminance1 + 0.05)).toFixed(2));
}
var hasBadDecorativeContrast = function hasBadDecorativeContrast(a, b) {
return getContrast(a, b) < 1.5;
return color2k.getContrast(a, b) < 1.5;
};
var hasBadReadableContrast = function hasBadReadableContrast(a, b) {
return getContrast(a, b) < 3;
return color2k.getContrast(a, b) < 3;
};
var hasBadAaContrast = function hasBadAaContrast(a, b) {
return getContrast(a, b) < 4.5;
return color2k.getContrast(a, b) < 4.5;
};
var hasBadAaaContrast = function hasBadAaaContrast(a, b) {
return getContrast(a, b) < 7;
return color2k.getContrast(a, b) < 7;
};

@@ -177,3 +45,3 @@

var surface = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#fff';
var readableContrast = readableColor(surface);
var readableContrast = color2k.readableColor(surface);
return {

@@ -237,2 +105,10 @@ original: color,

Object.keys(color2k).forEach(function (k) {
if (k !== 'default') Object.defineProperty(exports, k, {
enumerable: true,
get: function () {
return color2k[k];
}
});
});
exports.ColorContext = ColorContext;

@@ -244,5 +120,2 @@ exports.ColorContextProvider = ColorContextProvider;

exports.css = css;
exports.getContrast = getContrast;
exports.getLuminance = getLuminance;
exports.readableColor = readableColor;
exports.useColorContext = useColorContext;

@@ -249,0 +122,0 @@ exports.useTheme = useTheme;

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","sources":["../../packages/core/src/ColorContext.ts","../../packages/core/src/ColorContextProvider.tsx","../../packages/core/src/getLuminance.ts","../../packages/core/src/readableColor.ts","../../packages/core/src/getContrast.ts","../../packages/core/src/createReadablePalette.ts","../../packages/core/src/ThemeContext.ts","../../packages/core/src/ThemeProvider.tsx","../../packages/core/src/useColorContext.ts","../../packages/core/src/useTheme.ts","../../packages/core/src/css.ts"],"sourcesContent":["import { createContext } from 'react';\nimport { ColorContextValue } from './types';\n\nexport default createContext<ColorContextValue | null>(null);\n","import React, { useMemo } from 'react';\nimport ColorContext from './ColorContext';\n\ninterface Props {\n color: string;\n surface: string;\n children: React.ReactNode;\n}\n\nfunction ColorContextProvider({ color, surface, children }: Props) {\n const contextValue = useMemo(() => ({ color, surface }), [color, surface]);\n return (\n <ColorContext.Provider value={contextValue}>\n {children}\n </ColorContext.Provider>\n );\n}\n\nexport default ColorContextProvider;\n","import parseToRgba from '@ricokahler/parse-to-rgba';\n// taken from:\n// https://github.com/styled-components/polished/blob/0764c982551b487469043acb56281b0358b3107b/src/color/getLuminance.js\n\n/**\n * Returns a number (float) representing the luminance of a color.\n *\n * @example\n * // Styles as object usage\n * const styles = {\n * background: getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff',\n * background: getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?\n * 'rgba(58, 133, 255, 1)' :\n * 'rgba(255, 57, 149, 1)',\n * }\n *\n * // styled-components usage\n * const div = styled.div`\n * background: ${getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff'};\n * background: ${getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?\n * 'rgba(58, 133, 255, 1)' :\n * 'rgba(255, 57, 149, 1)'};\n *\n * // CSS in JS Output\n *\n * div {\n * background: \"#CCCD64\";\n * background: \"rgba(58, 133, 255, 1)\";\n * }\n */\nexport default function getLuminance(color: string): number {\n if (color === 'transparent') return 0;\n\n function f(x: number) {\n const channel = x / 255;\n return channel <= 0.03928\n ? channel / 12.92\n : ((channel + 0.055) / 1.055) ** 2.4;\n }\n\n const parsed = parseToRgba(color);\n\n if (!parsed) {\n throw new Error(`Could not parse color \"${color}\"`);\n }\n\n const [r, g, b] = parsed;\n return parseFloat((0.2126 * f(r) + 0.7152 * f(g) + 0.0722 * f(b)).toFixed(3));\n}\n","// taken from:\n// https://github.com/styled-components/polished/blob/6f72ff07eed0bb63ada17369fde4986c9389e5f9/src/color/readableColor.js\nimport getLuminance from './getLuminance';\n\n/**\n * Returns black or white for best contrast depending on the luminosity of the given color.\n */\nexport default function readableColor(color: string): string {\n return getLuminance(color) > 0.179 ? '#000' : '#fff';\n}\n","// taken from:\n// https://github.com/styled-components/polished/blob/0764c982551b487469043acb56281b0358b3107b/src/color/getContrast.js\nimport getLuminance from './getLuminance';\n\n/**\n * Returns the contrast ratio between two colors based on\n * [W3's recommended equation for calculating contrast](http://www.w3.org/TR/WCAG20/#contrast-ratiodef).\n *\n * @example\n * const contrastRatio = getContrast('#444', '#fff');\n */\nexport default function getContrast(color1: string, color2: string): number {\n const luminance1 = getLuminance(color1);\n const luminance2 = getLuminance(color2);\n return parseFloat(\n (luminance1 > luminance2\n ? (luminance1 + 0.05) / (luminance2 + 0.05)\n : (luminance2 + 0.05) / (luminance1 + 0.05)\n ).toFixed(2),\n );\n}\n","import readableColor from './readableColor';\nimport getContrast from './getContrast';\n\nimport { ReadableColorPalette } from './types';\n\nconst hasBadDecorativeContrast = (a: string, b: string) =>\n getContrast(a, b) < 1.5;\nconst hasBadReadableContrast = (a: string, b: string) => getContrast(a, b) < 3;\nconst hasBadAaContrast = (a: string, b: string) => getContrast(a, b) < 4.5;\nconst hasBadAaaContrast = (a: string, b: string) => getContrast(a, b) < 7;\n\nfunction createReadablePalette(\n color: string,\n surface = '#fff',\n): ReadableColorPalette {\n const readableContrast = readableColor(surface);\n\n return {\n original: color,\n decorative: hasBadDecorativeContrast(color, surface)\n ? readableContrast\n : color,\n readable: hasBadReadableContrast(color, surface) ? readableContrast : color,\n aa: hasBadAaContrast(color, surface) ? readableContrast : color,\n aaa: hasBadAaaContrast(color, surface) ? readableContrast : color,\n };\n}\n\nexport default createReadablePalette;\n","import { createContext } from 'react';\nexport default createContext<unknown>(null);\n","import React from 'react';\nimport ThemeContext from './ThemeContext';\n\ninterface Props {\n theme: unknown;\n children: React.ReactNode;\n}\n\nfunction ThemeProvider({ theme, children }: Props) {\n return (\n <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>\n );\n}\n\nexport default ThemeProvider;\n","import { useContext, useMemo } from 'react';\nimport ColorContext from './ColorContext';\nimport createReadablePalette from './createReadablePalette';\n\ninterface Props {\n color?: string;\n surface?: string;\n}\n\nfunction useColorContext(props?: Props) {\n const colorContext = useContext(ColorContext);\n\n if (!colorContext) {\n throw new Error(\n 'Could not find color context. Ensure this component is wrapped in a ColorContextProvider.',\n );\n }\n\n const color = props?.color || colorContext.color;\n const surface = props?.surface || colorContext.surface;\n\n return useMemo(\n () => ({\n color: createReadablePalette(color, surface),\n surface,\n }),\n [color, surface],\n );\n}\n\nexport default useColorContext;\n","import { useContext } from 'react';\nimport ThemeContext from './ThemeContext';\n\nfunction useTheme<T>(): T {\n const theme = useContext(ThemeContext) as T | null;\n\n if (!theme) {\n throw new Error(\n 'Could not find theme. Ensure this component is wrapped in a ThemeProvider',\n );\n }\n\n return theme;\n}\n\nexport default useTheme;\n","function css(strings: TemplateStringsArray, ...values: Array<string | number>) {\n let combined = '';\n\n for (let i = 0; i < strings.length; i += 1) {\n const currentString = strings[i];\n const currentValue = values[i] || '';\n combined += currentString + currentValue;\n }\n\n return combined;\n}\n\nexport default css;\n"],"names":["createContext","ColorContextProvider","color","surface","children","contextValue","useMemo","React","getLuminance","f","x","channel","parsed","parseToRgba","Error","r","g","b","parseFloat","toFixed","readableColor","getContrast","color1","color2","luminance1","luminance2","hasBadDecorativeContrast","a","hasBadReadableContrast","hasBadAaContrast","hasBadAaaContrast","createReadablePalette","readableContrast","original","decorative","readable","aa","aaa","ThemeProvider","theme","useColorContext","props","colorContext","useContext","ColorContext","useTheme","ThemeContext","css","strings","combined","i","length","currentString","currentValue"],"mappings":";;;;;;;;;AAGA,qBAAeA,mBAAa,CAA2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/C;EAEA,SAAOG,aAAO,CACZ;EAAA,WAAO;EACLJ,MAAAA,KAAK,EAAE6B,qBAAqB,CAAC7B,KAAD,EAAQC,OAAR,CADvB;EAELA,MAAAA,OAAO,EAAPA;EAFK,KAAP;EAAA,GADY,EAKZ,CAACD,KAAD,EAAQC,OAAR,CALY,CAAd;EAOD;;ECzBD,SAAS0C,QAAT,GAA0B;EACxB,MAAMN,KAAK,GAAGI,gBAAU,CAACG,YAAD,CAAxB;;EAEA,MAAI,CAACP,KAAL,EAAY;EACV,UAAM,IAAIzB,KAAJ,CACJ,2EADI,CAAN;EAGD;;EAED,SAAOyB,KAAP;EACD;;ECbD,SAASQ,GAAT,CAAaC,OAAb,EAA+E;EAC7E,MAAIC,QAAQ,GAAG,EAAf;;EAEA,OAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGF,OAAO,CAACG,MAA5B,EAAoCD,CAAC,IAAI,CAAzC,EAA4C;EAC1C,QAAME,aAAa,GAAGJ,OAAO,CAACE,CAAD,CAA7B;EACA,QAAMG,YAAY,GAAG,CAAOH,CAAP,gCAAOA,CAAP,6BAAOA,CAAP,UAAa,EAAlC;EACAD,IAAAA,QAAQ,IAAIG,aAAa,GAAGC,YAA5B;EACD;;EAED,SAAOJ,QAAP;EACD;;;;;;;;;;;;;;;;;;;;;;"}
{"version":3,"file":"index.js","sources":["../../packages/core/src/ColorContext.ts","../../packages/core/src/ColorContextProvider.tsx","../../packages/core/src/createReadablePalette.ts","../../packages/core/src/ThemeContext.ts","../../packages/core/src/ThemeProvider.tsx","../../packages/core/src/useColorContext.ts","../../packages/core/src/useTheme.ts","../../packages/core/src/css.ts"],"sourcesContent":["import { createContext } from 'react';\nimport { ColorContextValue } from './types';\n\nexport default createContext<ColorContextValue | null>(null);\n","import React, { useMemo } from 'react';\nimport ColorContext from './ColorContext';\n\ninterface Props {\n color: string;\n surface: string;\n children: React.ReactNode;\n}\n\nfunction ColorContextProvider({ color, surface, children }: Props) {\n const contextValue = useMemo(() => ({ color, surface }), [color, surface]);\n return (\n <ColorContext.Provider value={contextValue}>\n {children}\n </ColorContext.Provider>\n );\n}\n\nexport default ColorContextProvider;\n","import { readableColor, getContrast } from 'color2k';\nimport { ReadableColorPalette } from './types';\n\nconst hasBadDecorativeContrast = (a: string, b: string) =>\n getContrast(a, b) < 1.5;\nconst hasBadReadableContrast = (a: string, b: string) => getContrast(a, b) < 3;\nconst hasBadAaContrast = (a: string, b: string) => getContrast(a, b) < 4.5;\nconst hasBadAaaContrast = (a: string, b: string) => getContrast(a, b) < 7;\n\nfunction createReadablePalette(\n color: string,\n surface = '#fff',\n): ReadableColorPalette {\n const readableContrast = readableColor(surface);\n\n return {\n original: color,\n decorative: hasBadDecorativeContrast(color, surface)\n ? readableContrast\n : color,\n readable: hasBadReadableContrast(color, surface) ? readableContrast : color,\n aa: hasBadAaContrast(color, surface) ? readableContrast : color,\n aaa: hasBadAaaContrast(color, surface) ? readableContrast : color,\n };\n}\n\nexport default createReadablePalette;\n","import { createContext } from 'react';\nexport default createContext<unknown>(null);\n","import React from 'react';\nimport ThemeContext from './ThemeContext';\n\ninterface Props {\n theme: unknown;\n children: React.ReactNode;\n}\n\nfunction ThemeProvider({ theme, children }: Props) {\n return (\n <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>\n );\n}\n\nexport default ThemeProvider;\n","import { useContext, useMemo } from 'react';\nimport ColorContext from './ColorContext';\nimport createReadablePalette from './createReadablePalette';\n\ninterface Props {\n color?: string;\n surface?: string;\n}\n\nfunction useColorContext(props?: Props) {\n const colorContext = useContext(ColorContext);\n\n if (!colorContext) {\n throw new Error(\n 'Could not find color context. Ensure this component is wrapped in a ColorContextProvider.',\n );\n }\n\n const color = props?.color || colorContext.color;\n const surface = props?.surface || colorContext.surface;\n\n return useMemo(\n () => ({\n color: createReadablePalette(color, surface),\n surface,\n }),\n [color, surface],\n );\n}\n\nexport default useColorContext;\n","import { useContext } from 'react';\nimport ThemeContext from './ThemeContext';\n\nfunction useTheme<T>(): T {\n const theme = useContext(ThemeContext) as T | null;\n\n if (!theme) {\n throw new Error(\n 'Could not find theme. Ensure this component is wrapped in a ThemeProvider',\n );\n }\n\n return theme;\n}\n\nexport default useTheme;\n","function css(strings: TemplateStringsArray, ...values: Array<string | number>) {\n let combined = '';\n\n for (let i = 0; i < strings.length; i += 1) {\n const currentString = strings[i];\n const currentValue = values[i] || '';\n combined += currentString + currentValue;\n }\n\n return combined;\n}\n\nexport default css;\n"],"names":["createContext","ColorContextProvider","color","surface","children","contextValue","useMemo","React","hasBadDecorativeContrast","a","b","getContrast","hasBadReadableContrast","hasBadAaContrast","hasBadAaaContrast","createReadablePalette","readableContrast","readableColor","original","decorative","readable","aa","aaa","ThemeProvider","theme","useColorContext","props","colorContext","useContext","ColorContext","Error","useTheme","ThemeContext","css","strings","combined","i","length","currentString","currentValue"],"mappings":";;;;;;;;AAGA,qBAAeA,mBAAa,CAA2B,IAA3B,CAA5B;;ECMA,SAASC,oBAAT,OAAmE;EAAA,MAAnCC,KAAmC,QAAnCA,KAAmC;EAAA,MAA5BC,OAA4B,QAA5BA,OAA4B;EAAA,MAAnBC,QAAmB,QAAnBA,QAAmB;EACjE,MAAMC,YAAY,GAAGC,aAAO,CAAC;EAAA,WAAO;EAAEJ,MAAAA,KAAK,EAALA,KAAF;EAASC,MAAAA,OAAO,EAAPA;EAAT,KAAP;EAAA,GAAD,EAA6B,CAACD,KAAD,EAAQC,OAAR,CAA7B,CAA5B;EACA,sBACEI,6BAAC,YAAD,CAAc,QAAd;EAAuB,IAAA,KAAK,EAAEF;EAA9B,KACGD,QADH,CADF;EAKD;;ECbD,IAAMI,wBAAwB,GAAG,SAA3BA,wBAA2B,CAACC,CAAD,EAAYC,CAAZ;EAAA,SAC/BC,mBAAW,CAACF,CAAD,EAAIC,CAAJ,CAAX,GAAoB,GADW;EAAA,CAAjC;;EAEA,IAAME,sBAAsB,GAAG,SAAzBA,sBAAyB,CAACH,CAAD,EAAYC,CAAZ;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": "@react-style-system/core",
"version": "0.1.1",
"version": "0.2.0",
"author": {

@@ -25,5 +25,5 @@ "email": "ricokahler@me.com",

"dependencies": {
"@ricokahler/parse-to-rgba": "0.2.1",
"color2k": "1.0.0-rc.5",
"@babel/runtime": "7.9.6"
}
}

@@ -9,5 +9,3 @@ export { default as ColorContext } from './ColorContext';

export { default as css } from './css';
export { default as getContrast } from './getContrast';
export { default as getLuminance } from './getLuminance';
export { default as readableColor } from './readableColor';
export * from 'color2k';
export * from './types';
/**
* Returns the contrast ratio between two colors based on
* [W3's recommended equation for calculating contrast](http://www.w3.org/TR/WCAG20/#contrast-ratiodef).
*
* @example
* const contrastRatio = getContrast('#444', '#fff');
*/
export default function getContrast(color1: string, color2: string): number;
/**
* Returns a number (float) representing the luminance of a color.
*
* @example
* // Styles as object usage
* const styles = {
* background: getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff',
* background: getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?
* 'rgba(58, 133, 255, 1)' :
* 'rgba(255, 57, 149, 1)',
* }
*
* // styled-components usage
* const div = styled.div`
* background: ${getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff'};
* background: ${getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?
* 'rgba(58, 133, 255, 1)' :
* 'rgba(255, 57, 149, 1)'};
*
* // CSS in JS Output
*
* div {
* background: "#CCCD64";
* background: "rgba(58, 133, 255, 1)";
* }
*/
export default function getLuminance(color: string): number;
/**
* Returns black or white for best contrast depending on the luminosity of the given color.
*/
export default function readableColor(color: string): string;