How to use color-convert - 10 common examples

To help you get started, we’ve selected a few color-convert examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sahilchaddha / homebridge-magichome-platform / src / accessories / lightBulb.js View on Github external
}

      var colors = stdout.match(/\(.*,.*,.*\)/g)
      var isOn = stdout.match(/\] ON /g)
      if (isOn && isOn.length > 0) {
        settings.on = true
      }
      if (colors && colors.length > 0) {
        // Remove last char )
        var str = colors.toString().substring(0, colors.toString().length - 1)
        // Remove First Char (
        str = str.substring(1, str.length)
        const rgbColors = str.split(',').map((item) => {
          return item.trim()
        })
        var converted = convert.rgb.hsv(rgbColors)
        settings.color = {
          H: converted[0],
          S: converted[1],
          L: converted[2],
        }
      }

      callback(settings)
    })
  }
github TheAgentK / tuya-mqtt / tuya-color.js View on Github external
TuyaColorLight.prototype.setColor = function (colorValue) {
    debug("Recieved color", colorValue);

    if (this._ValIsHex(colorValue)) {
        debug("Color is Hex");
        var color = convert.hex.hsl(colorValue);
    } else {
        debug("Color is HSL");
        var color = colorValue.split(",");
        // convert strings to numbers
        color.forEach(function (element, key) {
            color[key] = parseInt(element, 10);
        });
    }
    debug("Converted color as HSL", {
        0: color[0] + " - " + typeof color[0],
        1: color[1] + " - " + typeof color[1],
        2: color[2] + " - " + typeof color[2]
    })

    this.setHSL(color[0], color[1], color[2]);
    return this.getDps();
github zerosoul / chinese-colors / src / utils.js View on Github external
export function getCorrectTextColor(rgb = [0, 0, 0]) {
  /*
  From this W3C document: http://www.webmasterworld.com/r.cgi?f=88&d=9769&url=http://www.w3.org/TR/AERT#color-contrast

  Color brightness is determined by the following formula:
  ((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000

  I know this could be more compact, but I think this is easier to read/explain.

  */
  if ((typeof rgb === 'string' || rgb instanceof String) && rgb.indexOf('#') > -1) {
    rgb = convert.hex.rgb(rgb);
  }
  console.log({ rgb });

  const threshold = 130; /* about half of 256. Lower threshold equals more dark text on dark background  */
  const [hRed, hGreen, hBlue] = rgb;

  const cBrightness = (hRed * 299 + hGreen * 587 + hBlue * 114) / 1000;
  if (cBrightness > threshold) {
    return '#50616d';
  } else {
    return '#e9f1f6';
  }
}
github zerosoul / rc-bullets / src / helper.js View on Github external
function getCorrectTextColor(rgb = [0, 0, 0]) {
  /*
  From this W3C document: http://www.webmasterworld.com/r.cgi?f=88&d=9769&url=http://www.w3.org/TR/AERT#color-contrast

  Color brightness is determined by the following formula:
  ((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000

  I know this could be more compact, but I think this is easier to read/explain.

  */
  if ((typeof rgb === 'string' || rgb instanceof String) && rgb.indexOf('#') > -1) {
    console.log('rgb is hex');
    rgb = convert.hex.rgb(rgb);
  } else if (typeof rgb === 'string') {
    console.log('rgb string', rgb);
    rgb = getRGB(rgb);
    console.log('rgb converted', rgb);
  }
  console.log({ rgb });

  const threshold = 130; /* about half of 256. Lower threshold equals more dark text on dark background  */
  const [hRed, hGreen, hBlue] = rgb;

  const cBrightness = (hRed * 299 + hGreen * 587 + hBlue * 114) / 1000;
  if (cBrightness > threshold) {
    return '#50616d';
  } else {
    return '#e9f1f6';
  }
github Zefau / ioBroker.hue-extended / hue-extended.js View on Github external
let lights = appliance.type == 'lights' ? [appliance.uid] : DEVICES['groups'][appliance.uid].lights;
			
			// handle color spaces
			let value = commands[action];
			let rgb = null, hsv = null;
			if (action == 'rgb')
			{
				rgb = value.split(',');
				hsv = _color.rgb.hsv(rgb);
			}
			
			else if (action == 'hsv')
				hsv = value.split(',');
			
			else if (action == 'cmyk')
				hsv = _color.cmyk.hsv(value.split(','));
			
			else if (action == 'xyz')
				hsv = _color.xyz.hsv(value.split(','));
			
			else if (action == 'hex')
				hsv = _color.hex.hsv(value.split(','));
			
			if (hsv !== null)
			{
				delete commands[action];
				commands = {
					'hue': hsv[0],
					'sat': Math.max(Math.min(Math.round(hsv[1]*2.54), 254), 0),
					'bri': Math.max(Math.min(Math.round(hsv[2]*2.54), 254), 0),
					...commands
				};
github Zefau / ioBroker.hue-extended / hue-extended.js View on Github external
let value = commands[action];
			let rgb = null, hsv = null;
			if (action == 'rgb')
			{
				rgb = value.split(',');
				hsv = _color.rgb.hsv(rgb);
			}
			
			else if (action == 'hsv')
				hsv = value.split(',');
			
			else if (action == 'cmyk')
				hsv = _color.cmyk.hsv(value.split(','));
			
			else if (action == 'xyz')
				hsv = _color.xyz.hsv(value.split(','));
			
			else if (action == 'hex')
				hsv = _color.hex.hsv(value.split(','));
			
			if (hsv !== null)
			{
				delete commands[action];
				commands = {
					'hue': hsv[0],
					'sat': Math.max(Math.min(Math.round(hsv[1]*2.54), 254), 0),
					'bri': Math.max(Math.min(Math.round(hsv[2]*2.54), 254), 0),
					...commands
				};
			}
			
			// check for each light, if hue lab scene is activated
github TylerBarnes / gatsby-plugin-transition-link / src / AniLink / PaintDrip.js View on Github external
createRipple = ({ length }, event, hex, color, node) => {
		const body = document.body
		const canvas = document.createElement('canvas')
		const ctx = canvas.getContext('2d')
		const piTwo = Math.PI * 2

		let rgb = hex ? convert.hex.rgb(hex).join(',') : '0,0,255'
		rgb = color ? convert.keyword.rgb(color) : rgb

		canvas.style.zIndex = 10000
		canvas.style.top = 0
		canvas.style.position = 'fixed'

		let vw = (canvas.width = window.innerWidth)
		let vh = (canvas.height = window.innerHeight)

		body.appendChild(canvas)

		// Event coords
		const x = event.clientX
		const y = event.clientY

		// Delta - difference between event and farthest corner
github mattmattmatt / node-red-contrib-yeelight-compat-hue / src / YeeLightNodeOut.js View on Github external
.then(state => {
                    let colorValue;
                    let colorMode;
                    const currentState = sanitizeState(state).state;
                    let briToTurnTo = clamp(normalize(bri || currentState.bri, 255, 100), 1, 100);

                    if (typeof ct !== 'undefined') {
                        colorMode = 2;
                        colorValue = ct;
                    } else if (typeof hex !== 'undefined') {
                        colorMode = 1;
                        colorValue = hexToRgbInt(hex);
                        // if no bri was specified, calculate from hex value
                        briToTurnTo = bri ? briToTurnTo : clamp(convert.hex.hsv(hex)[2], 1, 100);
                    } else if (
                        typeof hue !== 'undefined' ||
                        typeof sat !== 'undefined' ||
                        typeof bri !== 'undefined'
                    ) {
                        colorMode = 1;
                        colorValue = hexToRgbInt(
                            convert.hsv.hex(
                                normalize(hue || currentState.hue, 65535, 359),
                                normalize(sat || currentState.sat, 255, 100),
                                briToTurnTo
                            )
                        );
                    } else if (on) {
                        return node.serverConfig.yeelight.set_power(on, null, duration);
                    }
github drumfreak / homebridge-tuya-colorlight / index.js View on Github external
TuyaColorLight.prototype.setToCurrentColor = function(callback) {
  if(this.deviceEnabled === false) {
    this.log.warn('Device is disabled... Bailing out...');
    callback('Disabled');
    return;
  }

  var color = this.color;
  var color2 = this.color2;

  var lightness = Math.round(this.brightness / 2);
  var brightness = this.brightness;
  var apiBrightness = this._convertPercentageToVal(brightness);
  var alphaBrightness = this._getAlphaHex(brightness);

  var hexColorOriginal1 = convert.hsl.hex(color.H, color.S, color.L);
  var rgbColorOriginal1 = convert.hsl.rgb(color.H, color.S, color.L);

  var hexColorOriginal2 = convert.hsl.hex(0, 0, 50);
  var rgbColorOriginal2 = convert.hsl.rgb(0, 0, 50);

  var hexColor1 = convert.hsl.hex(color.H, color.S, lightness);
  var rgbColor1 = convert.hsl.rgb(color.H, color.S, lightness);

  var hexColor2 = convert.hsl.hex(0, 0, lightness);
  var rgbColor2 = convert.hsl.rgb(0, 0, lightness);

  var colorTemperature = this.colorTemperature;

  // var ww = Math.round((this.brightness * 255) / 100);

  var lightColor = (hexColor1  + hexColor2 + alphaBrightness).toLowerCase();
github TheAgentK / tuya-mqtt / tuya-color.js View on Github external
TuyaColorLight.prototype.getDps = function () {
    var color = this.color;

    var lightness = Math.round(this.brightness / 2);
    var brightness = this.brightness;
    var apiBrightness = this._convertPercentageToVal(brightness);
    var alphaBrightness = this._getAlphaHex(brightness);

    var hexColor1 = convert.hsl.hex(color.H, color.S, lightness);

    var hexColor2 = convert.hsl.hex(0, 0, lightness);

    var colorTemperature = this.colorTemperature;

    var lightColor = (hexColor1 + hexColor2 + alphaBrightness).toLowerCase();

    var temperature = (this.colorMode === 'colour') ? 255 : this._convertColorTemperature(colorTemperature);

    dpsTmp = {
        '1': true,
        '2': this.colorMode,
        '3': apiBrightness,
        '4': temperature,
        '5': lightColor
        // '6' : hexColor + hexColor + 'ff'
    };
    debug("dps", dpsTmp);