diff --git a/types/color-check/color-check-tests.ts b/types/color-check/color-check-tests.ts new file mode 100644 index 0000000000..9d04819f28 --- /dev/null +++ b/types/color-check/color-check-tests.ts @@ -0,0 +1,25 @@ +import colorCheck, { RGB, Color } from 'color-check'; + +const r1: RGB = { r: 1, g: 2, b: 3 }; +const r2: RGB = { r: '1', g: '2', b: '3' }; +const c1: Color = { r: 3, g: '5', b: 244 }; +const c2: Color = '#ffffff'; + +// $ExpectType boolean +colorCheck.colorDifference(r1, c1); +// $ExpectType boolean +colorCheck.colorDifference(c1, r1); +// $ExpectError +colorCheck.colorDifference(r1, undefined); + +// $ExpectType number +colorCheck.colorContrast(r2, c2); +// $ExpectType number +colorCheck.colorContrast(c2, r2); + +// $ExpectType number +colorCheck.colorGetLuminance([1, 2, 3]); +// $ExpectError +colorCheck.colorGetLuminance(r1); +// $ExpectError +colorCheck.colorGetLuminance([1, 2, 3, 4]); diff --git a/types/color-check/index.d.ts b/types/color-check/index.d.ts new file mode 100644 index 0000000000..37af87b35c --- /dev/null +++ b/types/color-check/index.d.ts @@ -0,0 +1,99 @@ +// Type definitions for color-check 0.0 +// Project: https://github.com/motleydev/color-check#readme +// Definitions by: Elizabeth Craig +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +export interface RGB { + r: string | number; + g: string | number; + b: string | number; +} + +export type Color = string | RGB; + +export const brightnessThreshold: number; +export const colorContrastThreshold: number; + +/** + * Checks if a hex string or object was given and returns the rgb color values of the former. + * To keep a tiny library, it's the user's responsibility to provide valid hex/rgbObj values. + * @param colorValue Color to convert + * @returns Returns an object of shape {r, g, b} + */ +export function hexToRgb(colorValue: Color): RGB; + +/** + * Checks if the two colors have significant color difference. + * @param foreground Foreground color + * @param background Background color + * @returns The color difference, which must be 500 or greater + */ +export function colorDifference(foreground: Color, background: Color): boolean; + +/** + * Checks if two colors have enough brightness difference. + * @param foreground Foreground color + * @param background Background color + * @returns The brightness difference, which must be over 125 + */ +export function colorBrightnessDifference(foreground: Color, background: Color): boolean; + +/** + * Gets the total luminance from a color. + * @param rgb The color, represented as an array of three RGB components (0-255 inclusive). + * @returns The color's luminance + */ +export function colorGetLuminance(rgb: [number, number, number]): number; + +/** + * Gets the contrast between two colors. + * @param foreground Foreground color + * @param background Background color + * @returns The contrast value + */ +export function colorContrast(foreground: Color, background: Color): number; + +/** + * Checks if two colors are both color compliant and contrast compliant. + * @param foreground Foreground color + * @param background Background color + * @returns Whether the colors are compliant + */ +export function colorCompliance(foreground: Color, background: Color): boolean; + +/** + * Checks whether the difference between two colors is acceptable for AA standards for + * legibility of size 14pt font. + * @param foreground Foreground color + * @param background Background color + * @returns Whether the color difference is acceptable + */ +export function aa(foreground: Color, background: Color): boolean; + +/** + * Checks whether the difference between two colors is acceptable for AA standards for + * legibility of size 18pt font. + * @param foreground Foreground color + * @param background Background color + * @returns Whether the color difference is acceptable + */ +export function aa_18(foreground: Color, background: Color): boolean; + +/** + * Checks whether the difference between two colors is acceptable for AAA standards for + * legibility of size 14pt font (this is very hard to achieve). + * @param foreground Foreground color + * @param background Background color + * @returns Whether the color difference is acceptable + */ +export function aaa(foreground: Color, background: Color): boolean; + +/** + * Checks whether the difference between two colors is acceptable for AAA standards for + * legibility of size 18pt font (this is very hard to achieve). + * @param foreground Foreground color + * @param background Background color + * @returns Whether the color difference is acceptable + */ +export function aaa_18(foreground: Color, background: Color): boolean; diff --git a/types/color-check/tsconfig.json b/types/color-check/tsconfig.json new file mode 100644 index 0000000000..33b4b67a72 --- /dev/null +++ b/types/color-check/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es5" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "esModuleInterop": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "color-check-tests.ts" + ] +} diff --git a/types/color-check/tslint.json b/types/color-check/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/color-check/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }