index.js
function(red, green, blue, model) {
    red = red / 255;
    green = green / 255;
    blue = blue / 255;
    var r = red > 0.04045 ? Math.pow(((red + 0.055) / 1.055), 2.4000000953674316) : red / 12.92;
    var g = green > 0.04045 ? Math.pow(((green + 0.055) / 1.055), 2.4000000953674316) : green / 12.92;
    var b = blue > 0.04045 ? Math.pow(((blue + 0.055) / 1.055), 2.4000000953674316) : blue / 12.92;
    var x = r * 0.664511 + g * 0.154324 + b * 0.162028;
    var y = r * 0.283881 + g * 0.668433 + b * 0.047685;
    var z = r * 8.8E-5 + g * 0.07231 + b * 0.986039;
    var xy = [x / (x + y + z), y / (x + y + z)];
    if (isNaN(xy[0])) {
      xy[0] = 0.0;
    }
    
    if (isNaN(xy[1])) {
      xy[1] = 0.0;
    }
    
    var colorPoints = colorPointsForModel(model);
    var inReachOfLamps = checkPointInLampsReach(xy, colorPoints);
    if (!inReachOfLamps) {
      var pAB = getClosestPointToPoints(colorPoints[0], colorPoints[1], xy);
      var pAC = getClosestPointToPoints(colorPoints[2], colorPoints[0], xy);
      var pBC = getClosestPointToPoints(colorPoints[1], colorPoints[2], xy);
      var dAB = getDistanceBetweenTwoPoints(xy, pAB);
      var dAC = getDistanceBetweenTwoPoints(xy, pAC);
      var dBC = getDistanceBetweenTwoPoints(xy, pBC);
      var lowest = dAB;
      var closestPoint = pAB;
      if (dAC < dAB) {
        lowest = dAC;
        closestPoint = pAC;
      }
      
      if (dBC < lowest) {
        closestPoint = pBC;
      }
      
      xy[0] = closestPoint[0];
      xy[1] = closestPoint[1];
    }
    
    xy[0] = precision(xy[0]);
    xy[1] = precision(xy[1]);
    return xy;
  }
function(red, green, blue, model) {
    red = red / 255;
    green = green / 255;
    blue = blue / 255;
    var r = red > 0.04045 ? Math.pow(((red + 0.055) / 1.055), 2.4000000953674316) : red / 12.92;
    var g = green > 0.04045 ? Math.pow(((green + 0.055) / 1.055), 2.4000000953674316) : green / 12.92;
    var b = blue > 0.04045 ? Math.pow(((blue + 0.055) / 1.055), 2.4000000953674316) : blue / 12.92;
    var x = r * 0.664511 + g * 0.154324 + b * 0.162028;
    var y = r * 0.283881 + g * 0.668433 + b * 0.047685;
    var z = r * 8.8E-5 + g * 0.07231 + b * 0.986039;
    var xy = [x / (x + y + z), y / (x + y + z)];
    if (isNaN(xy[0])) {
      xy[0] = 0.0;
    }
    
    if (isNaN(xy[1])) {
      xy[1] = 0.0;
    }
    
    var colorPoints = colorPointsForModel(model);
    var inReachOfLamps = checkPointInLampsReach(xy, colorPoints);
    if (!inReachOfLamps) {
      var pAB = getClosestPointToPoints(colorPoints[0], colorPoints[1], xy);
      var pAC = getClosestPointToPoints(colorPoints[2], colorPoints[0], xy);
      var pBC = getClosestPointToPoints(colorPoints[1], colorPoints[2], xy);
      var dAB = getDistanceBetweenTwoPoints(xy, pAB);
      var dAC = getDistanceBetweenTwoPoints(xy, pAC);
      var dBC = getDistanceBetweenTwoPoints(xy, pBC);
      var lowest = dAB;
      var closestPoint = pAB;
      if (dAC < dAB) {
        lowest = dAC;
        closestPoint = pAC;
      }
      
      if (dBC < lowest) {
        closestPoint = pBC;
      }
      
      xy[0] = closestPoint[0];
      xy[1] = closestPoint[1];
    }
    
    xy[0] = precision(xy[0]);
    xy[1] = precision(xy[1]);
    return xy;
  }
function ( red , green , blue , model ) { red = red / 255 ; green = green / 255 ; blue = blue / 255 ; var r = red > 0.04045 ? Math . pow ( ( ( red + 0.055 ) / 1.055 ) , 2.4000000953674316 ) : red / 12.92 ; var g = green > 0.04045 ? Math . pow ( ( ( green + 0.055 ) / 1.055 ) , 2.4000000953674316 ) : green / 12.92 ; var b = blue > 0.04045 ? Math . pow ( ( ( blue + 0.055 ) / 1.055 ) , 2.4000000953674316 ) : blue / 12.92 ; var x = r * 0.664511 + g * 0.154324 + b * 0.162028 ; var y = r * 0.283881 + g * 0.668433 + b * 0.047685 ; var z = r * 8.8E-5 + g * 0.07231 + b * 0.986039 ; var xy = [ x / ( x + y + z ) , y / ( x + y + z ) ] ; if ( isNaN ( xy [ 0 ] ) ) { xy [ 0 ] = 0.0 ; } if ( isNaN ( xy [ 1 ] ) ) { xy [ 1 ] = 0.0 ; } var colorPoints = colorPointsForModel ( model ) ; var inReachOfLamps = checkPointInLampsReach ( xy , colorPoints ) ; if ( ! inReachOfLamps ) { var pAB = getClosestPointToPoints ( colorPoints [ 0 ] , colorPoints [ 1 ] , xy ) ; var pAC = getClosestPointToPoints ( colorPoints [ 2 ] , colorPoints [ 0 ] , xy ) ; var pBC = getClosestPointToPoints ( colorPoints [ 1 ] , colorPoints [ 2 ] , xy ) ; var dAB = getDistanceBetweenTwoPoints ( xy , pAB ) ; var dAC = getDistanceBetweenTwoPoints ( xy , pAC ) ; var dBC = getDistanceBetweenTwoPoints ( xy , pBC ) ; var lowest = dAB ; var closestPoint = pAB ; if ( dAC < dAB ) { lowest = dAC ; closestPoint = pAC ; } if ( dBC < lowest ) { closestPoint = pBC ; } xy [ 0 ] = closestPoint [ 0 ] ; xy [ 1 ] = closestPoint [ 1 ] ; } xy [ 0 ] = precision ( xy [ 0 ] ) ; xy [ 1 ] = precision ( xy [ 1 ] ) ; return xy ; }
Calculate XY color points for a given RGB value.
@param {number} red RGB red value (0-255)
@param {number} green RGB green value (0-255)
@param {number} blue RGB blue value (0-255)
@param {string} model Hue bulb model
@returns {number[]}
Calculate XY color points for a given RGB value .