PhilipsHue / HueSDK

Philips Hue Software Development Kit
98 stars 39 forks source link

Conversion from PHSColor to NSColor for app display. #11

Closed aaronn closed 6 years ago

aaronn commented 6 years ago

Is there any easy way to convert a PHSColor that might be in Mired CT, HSL, or XY into an NSColor or UIColor object for preview usage in an app?

jhvdb87 commented 6 years ago

Example of XY to UIColor:

- (UIColor *)colorForLight:(PHSLightPoint *)lightPoint {
       NSString *modelId = lightPoint.lightConfiguration.modelId;
       NSString *swVersion = lightPoint.lightConfiguration.swVersion;
       PHSNumberPair *xyNumberPair = lightState.xyColor;

       if (xyNumberPair.value1.floatValue > 0.0f && xyNumberPair.value2.floatValue > 0.0f ) {
           PHSColorXY *xyColor = [[PHSColorXY alloc] initWithX:xyNumberPair.value1.doubleValue Y:xyNumberPair.value2.doubleValue];
           PHSColor *colorConversion = [PHSColor initWithAppXY:xyColor brightness:lightState.brightness.doubleValue model:modelId swVersion:swVersion];
           PHSColorRGB *rgbColor = colorConversion.rgb;
           return [UIColor colorWithRed:(rgbColor.red / 255.0f) green:(rgbColor.green / 255.0f) blue:(rgbColor.blue / 255.0f) alpha:1.0f];
       }

       return nil;
}

Please let me now if you need further assistance.