UNPKG

hue-hacking-node

Version:

Utility to control Philips Hue light bulbs

188 lines (187 loc) 7.08 kB
/** * Color utility functions, exposed as a Typescript class. * No external dependencies. * Special thanks for the RGB to CIE conversion code goes out to the Q42 team * for their Q42.HueApi work. Dank u! * More info: https://github.com/Q42/Q42.HueApi. * * https://github.com/bjohnso5/hue-hacking * Copyright (c) 2013 Bryan Johnson; Licensed MIT */ import { RGB, XYPoint } from './hue-interfaces.js'; export declare const CIERed: XYPoint; export declare const CIELime: XYPoint; export declare const CIEBlue: XYPoint; export declare const hexFullRed = "FF0000"; export declare const hexFullGreen = "00FF00"; export declare const hexFullBlue = "0000FF"; export declare const hexFullWhite = "FFFFFF"; export declare class HueColors { constructor(); private cssColors; /** * Parses a valid hex color string and returns the Red RGB integer value. * * @param {string} hex Hex color string. * @return {number} Red integer value. */ private hexToRed; /** * Parses a valid hex color string and returns the Green RGB integer value. * * @param {string} hex Hex color string. * @return {number} Green integer value. */ private hexToGreen; /** * Parses a valid hex color string and returns the Blue RGB integer value. * * @param {string} hex Hex color string. * @return {number} Blue integer value. */ private hexToBlue; /** * Converts a valid hex color string to an RGB array. * * @param {string} h Hex color String (e.g. FF00FF) * @return {Array<number>} Array containing R, G, B values */ private hexToRGB; /** * Converts an RGB component to a hex string. * * @param {number} c RGB value, integer between 0 and 255. * @returns {string} Hex value string (e.g. FF) */ private componentToHex; /** * Converts RGB color components to a valid hex color string. * * @param {RGB} rgb RGB components with values between 0 and 255. * @returns {string} Hex color string (e.g. FF0000) */ private rgbToHex; /** * Generates a random number between 'from' and 'to'. * * @param {number} from Number representing the start of a range. * @param {number} to Number representing the end of a range. */ private randomFromInterval; /** * Return a random Integer in the range of 0 to 255, representing an RGB * color value. * * @return {number} Integer between 0 and 255. */ private randomRGBValue; /** * Returns the cross product of two XYPoints. * * @param {XYPoint} p1 Point 1 * @param {XYPoint} p2 Point 2 * @return {number} Cross-product of the two XYPoints provided */ private crossProduct; /** * Check if the provided XYPoint can be recreated by a Hue lamp. * * @param {XYPoint} p XYPoint to check * @return {boolean} Flag indicating if the point is within reproducible range */ private checkPointInLampsReach; /** * Find the closest point on a line. This point will be reproducible by a Hue lamp. * * @param {XYPoint} A The point where the line starts * @param {XYPoint} B The point where the line ends * @param {XYPoint} P The point which is close to the line * @return {XYPoint} A point that is on the line, and closest to the XYPoint provided */ private getClosestPointToLine; /** * Find the closest Hue-producible point to a provided point. * * @param {XYPoint} xyPoint The point to find the closest reproducible point to * @return {XYPoint} The closest Hue-reproducible point to the provided point */ private getClosestPointToPoint; /** * Returns the distance between two XYPoints. * * @param {XYPoint} one The first point * @param {XYPoint} two The second point * @return {number} The distance between points one and two */ private getDistanceBetweenTwoPoints; /** * Returns an XYPoint object containing the closest available CIE 1931 * coordinates based on the RGB input values. * * @param {RGB} rgb RGB color object * @return {XYPoint} CIE 1931 XY coordinates, corrected for reproducibility */ private getXYPointFromRGB; /** * Returns a rgb array for given x, y values. Not actually an inverse of * getXYPointFromRGB. Implementation of the instructions found on the * Philips Hue iOS SDK docs: http://goo.gl/kWKXKl * * @param {XYPoint} coords CIE 1931 x,y coordinates * @param {number} bri Brightness value between 0 and 1 * @return {RGB} RGB color object */ private getRGBFromXYAndBrightness; /** * Converts hexadecimal colors represented as a String to approximate * CIE 1931 coordinates. May not produce accurate values. * * @param {string} h Value representing a hexadecimal color value * @return {XYPoint} Approximate CIE 1931 x,y coordinates */ hexToCIE1931(h: string): XYPoint; /** * Converts red, green and blue integer values to approximate CIE 1931 * x and y coordinates. Algorithm from: * http://www.easyrgb.com/index.php?X=MATH&H=02#text2. May not produce * accurate values. * * @param {RGB} rgb RGB color object * @return {XYPoint} Approximate CIE 1931 x,y coordinates */ rgbToCIE1931(rgb: RGB): XYPoint; /** * Returns the approximate CIE 1931 x,y coordinates represented by the * supplied hexColor parameter, or of a random color if the parameter * is not passed. * * @param {string} hexColor String representing a hexidecimal color value OR a named CSS color (e.g. "red", "yellow", etc.) * @return {XYPoint} Approximate CIE 1931 x,y coordinates */ getCIEColor(hexColor?: string): XYPoint; /** * Returns the approximate hexColor represented by the supplied * CIE 1931 x,y coordinates and brightness value. * * @param {XYPoint} coords CIE 1931 coordinates * @param {number} bri value expressed between 0 and 1 * @return {string} hex color string */ CIE1931ToHex(coords: XYPoint, bri?: number): string; /** * Returns the approximate RGB values represented by the supplied * CIE 1931 x,y coordinates and brightness value. * * @param {XYPoint} coords CIE 1931 coordinates * @param {number} bri Brightness value expressed between 0 and 1 * @return {string} CSS rgb color expression */ CIE1931ToRGB(coords: XYPoint, bri?: number): RGB; /** * Convert a Mired temperature to its Kelvin equivalent. */ miredToKelvin(mired: number): number; /** * Convert a Kelvin temperature to its Mired equivalent. */ kelvinToMired(kelvin: number): number; }