Abhishek Jain
Tech Insider
Published in
3 min readJan 9, 2018

--

Playing with Philips HUE Using Android Palette API (changing bulb color from live streaming video)

Philips Hue Bulb Kit

USE CASE :We will be extracting color from an image or (fetching bitmap from a video)then fetching prominent colors from that bitmap and those colors will be used to change bulb color.

1) Understanding palette API

The palette library is a support library that extracts prominent colors from images to help you create visually engaging apps.

The Palette object gives you access to the colors in a Bitmap image while also providing six main color profiles from the bitmap to help inform your design choices.

android {
compileSdkVersion 24
...
}

dependencies {
...
compile 'com.android.support:palette-v7:24.2.1'
}

A Palette object gives you access to the primary colors in an image, as well as the corresponding colors for overlaid text.

(i) Generate a Palette instance using Palette’s from(Bitmap bitmap)

public void createPaletteAsync(Bitmap bitmap) {
Palette.from(bitmap).generate(new PaletteAsyncListener() {
public void onGenerated(Palette p) {
// Use generated instance
}
});
}

2) Now fetching bitmap from live running video (changing colors according to the video brightness changes)(fetching from video view)

// Android VideoView is used to stream videohandler = new Handler();
runningCode = new Runnable() {
@Override
public void run() {
int currentPosition = videoView.getCurrentPosition();
Bitmap bmFrame = mediaMetadataRetriever.getFrameAtTime(currentPosition * 1000,
MediaMetadataRetriever.OPTION_CLOSEST);
//units in microsecond
if (bmFrame != null) {
extractColor(bmFrame);
}
handler.postDelayed(runningCode, 1000);
}

passing the bitmap fetched above to method extractColor(bitmap)

The palette library attempts to extract the following six color profiles:

  • Light Vibrant
  • Vibrant
  • Dark Vibrant
  • Light Muted
  • Muted
  • Dark Muted
private void extractColor(final Bitmap bmp){
// Retrieves Palette Colors here
Palette.from(bmp).generate(new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
int defaultVal = 0xFFFFFF;

int vibrantColor = palette.getVibrantColor(defaultVal);
int lightVibrantColor = palette.getLightVibrantColor(defaultVal);
int darkVibrantColor = palette.getDarkVibrantColor(defaultVal);
int mutedColor = palette.getMutedColor(defaultVal);
int lightMutedColor = palette.getLightMutedColor(defaultVal);
int darkMutedColor = palette.getDarkMutedColor(defaultVal);

}
});
}
//these colors can be used to set bulb colors

3 Understanding Philips Hue sdk and implementation

first we have to authenticate our device using button present in the hub.

For the implementation up to that part, use sample application(authentication)

public void lightHue(color){

PHBridge bridge = phHueSDK.getSelectedBridge();
// it will give the list of the phlights connected to hub
List<PHLight> allLight =bridg.getResourceCache().getAllLights();

for (PHLight ligh : allLight) {
PHLightState lightStat = new PHLightState();

//lightStat.setHue(color);
// int r = (color & 0xFF0000) >> 16;
// int g = (color & 0xFF00) >> 8;
// int b = (color & 0xFF);

//To set lights to specific RGB Colors:

float xy[] = PHUtilities.calculateXY(dom, ligh.getModelNumber());

lightStat.setX(xy[0]);
lightStat.setY(xy[1]);
bridg.updateLightState(ligh, lightStat, listener);
}

//read java documentation for more methods

4) Understanding Colors dependency

Source : http://www.developers.meethue.com/sites/default/files/gamut_0.png

You can see three color gamuts in the image above ,every device can emit color range depending on the gamut they lie ,to find that out

  1. to get model id follow:
PHBridge bridge = phHueSDK.getSelectedBridge();
List<PHLight> allLight = bridg.getResourceCache().getAllLights();
for (PHLight light : allLight) {
light.getModelNumber();
}

Then match the value from the table at link below and you will get to know colors supported by bulb https://developers.meethue.com/documentation/supported-lights

References:

  1. https://developer.android.com/training/material/palette-colors.html
  2. https://github.com/PhilipsHue/PhilipsHueSDK-Java-MultiPlatform-Android/tree/master/QuickStartApp
  3. https://developers.meethue.com/documentation/java-sdk-getting-started

--

--

Abhishek Jain
Tech Insider

Android dev | Software developer at InMobi, Ex- MMT, Tokopedia