Skip to content

Commit

Permalink
Removed all references to AWT color, switched to int, loefberg#23
Browse files Browse the repository at this point in the history
  • Loading branch information
iheart2code committed Feb 11, 2015
1 parent 22f73de commit 65f07fe
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 47 deletions.
5 changes: 2 additions & 3 deletions src/main/java/com/github/besherman/lifx/LFXGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package com.github.besherman.lifx;

import java.awt.Color;
import java.beans.PropertyChangeListener;

/**
Expand Down Expand Up @@ -85,14 +84,14 @@ public interface LFXGroup extends LFXLightCollection {
/**
* Sets the color of all lights in this group.
*/
void setColor(Color color);
void setColor(int color);

/**
* Sets the color of all lights in this group.
* @param color
* @param duration the fade time in milliseconds
*/
void setColor(Color color, long duration);
void setColor(int color, long duration);

/**
* Adds a light to this group.
Expand Down
54 changes: 46 additions & 8 deletions src/main/java/com/github/besherman/lifx/LFXHSBKColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
package com.github.besherman.lifx;

import java.awt.Color;

/**
* Color described by hue, saturation, brightness and kelvin.
*
Expand All @@ -43,7 +41,7 @@ public class LFXHSBKColor {
/**
* Creates a new color with a kelvin of 6500 (to match D6500).
*/
public LFXHSBKColor(Color color) {
public LFXHSBKColor(int color) {
this(color, 6500);
}

Expand All @@ -52,14 +50,12 @@ public LFXHSBKColor(Color color) {
* @param color an RGB color
* @param kelvin the kelvin value in the range [0, 10000]
*/
public LFXHSBKColor(Color color, int kelvin) {
if(color == null) {
throw new IllegalArgumentException("color can not be null");
}
public LFXHSBKColor(int color, int kelvin) {
if(kelvin < MIN_KELVIN || kelvin > MAX_KELVIN) {
throw new IllegalArgumentException("kelvin must be between 0 and 10 000");
}
float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);

float[] hsb = LFXHSBKColor.RGBtoHSB(color, null);
this.hue = hsb[0] * 360;
this.saturation = hsb[1];
this.brightness = hsb[2];
Expand Down Expand Up @@ -200,4 +196,46 @@ public boolean equals(Object obj) {
}
return true;
}

// source: java.awt.Color
public static float[] RGBtoHSB(int color, float[] hsbvals) {
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;

float hue, saturation, brightness;
if (hsbvals == null) {
hsbvals = new float[3];
}
int cmax = (r > g) ? r : g;
if (b > cmax) cmax = b;
int cmin = (r < g) ? r : g;
if (b < cmin) cmin = b;

brightness = ((float) cmax) / 255.0f;
if (cmax != 0)
saturation = ((float) (cmax - cmin)) / ((float) cmax);
else
saturation = 0;
if (saturation == 0)
hue = 0;
else {
float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
if (r == cmax)
hue = bluec - greenc;
else if (g == cmax)
hue = 2.0f + redc - bluec;
else
hue = 4.0f + greenc - redc;
hue = hue / 6.0f;
if (hue < 0)
hue = hue + 1.0f;
}
hsbvals[0] = hue;
hsbvals[1] = saturation;
hsbvals[2] = brightness;
return hsbvals;
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/github/besherman/lifx/LFXLight.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package com.github.besherman.lifx;

import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.util.Date;

Expand Down Expand Up @@ -85,14 +84,14 @@ public interface LFXLight {
/**
* Sets the lights color.
*/
void setColor(Color color);
void setColor(int color);

/**
* Sets the lights color over time.
* @param color the color to set.
* @param duration the fade time in milliseconds.
*/
void setColor(Color color, long duration);
void setColor(int color, long duration);

/**
* Returns the brightness component of the current color.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.github.besherman.lifx.LFXClient;
import com.github.besherman.lifx.LFXHSBKColor;
import com.github.besherman.lifx.LFXLight;
import java.awt.Color;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -49,7 +48,7 @@ public static void main(String[] args) throws Exception {
LFXAlarmCollection alarms = light.getAlarms();
if(alarms.size() > 0) {
System.out.format("Adding alarm to light '%s': %n", light.getLabel());
alarms.set(0, new LFXAlarm(time, new LFXHSBKColor(Color.BLUE), 7 * 1000));
alarms.set(0, new LFXAlarm(time, new LFXHSBKColor(0xFF0000FF), 7 * 1000));
} else {
Logger.getLogger(AlarmEx02SetAlarm.class.getName()).log(Level.INFO, "Alarms was not loaded");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.github.besherman.lifx.LFXClient;
import com.github.besherman.lifx.LFXHSBKColor;
import com.github.besherman.lifx.LFXLight;
import java.awt.Color;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -52,7 +52,7 @@ public static void main(String[] args) throws Exception {
LFXAlarmCollection alarms = light.getAlarms();
if(alarms.size() > 0) {
System.out.format("Adding alarm to light '%s': %n", light.getLabel());
alarms.set(0, new LFXAlarm(time, new LFXHSBKColor(Color.BLUE), 10 * 1000));
alarms.set(0, new LFXAlarm(time, new LFXHSBKColor(0xFF0000FF), 10 * 1000));
} else {
Logger.getLogger(AlarmEx03SetAlarm.class.getName()).log(Level.INFO, "Alarms was not loaded");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.github.besherman.lifx.LFXClient;
import com.github.besherman.lifx.LFXGroup;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -42,7 +41,7 @@ public static void main(String[] args) throws Exception {
Logger.getLogger(GroupEx07ChangeColor.class.getName()).log(Level.INFO, "No test group found");
return;
}
group.setColor(Color.MAGENTA);
group.setColor(0xFFFF00FF);
} finally {
client.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.github.besherman.lifx.LFXClient;
import com.github.besherman.lifx.LFXGroup;
import java.awt.Color;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -61,24 +60,24 @@ public static void main(String[] args) throws Exception {

private static class Task extends TimerTask {
private final LFXGroup group;
private final List<Color> colors = Arrays.asList(
Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA,
Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW);
private final List<Number> colors = Arrays.asList(
(Number) 0xFF0000FF, (Number) 0xFF00FFFF, (Number) 0xFF00FF00, (Number) 0xFFFF00FF,
(Number) 0xFFFFA500, (Number) 0xFFFF69B4, (Number) 0xFFFF0000, (Number) 0xFFFFFF00);
private final Random random = new Random();
private volatile Color last;
private volatile Number last;

public Task(LFXGroup group) {
this.group = group;
}

@Override
public void run() {
Color color = null;
Number color = null;
do {
color = colors.get(random.nextInt(colors.size()));
} while(color == last);
} while(color.equals(last));

group.setColor(color, 0);
group.setColor(color.intValue(), 0);
last = color;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.github.besherman.lifx.LFXClient;
import com.github.besherman.lifx.LFXLight;
import java.awt.Color;

/**
* Changes the color on all lights.
Expand All @@ -38,7 +37,7 @@ public static void main(String[] args) throws Exception {
for(LFXLight light: client.getLights()) {
System.out.format("Setting color on '%s' %n", light.getLabel());
light.setPower(true);
light.setColor(Color.PINK);
light.setColor(0xFFFF69B4);
}
} finally {
client.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.github.besherman.lifx.LFXClient;
import com.github.besherman.lifx.LFXLight;
import com.github.besherman.lifx.LFXLightCollection;
import java.awt.Color;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
Expand All @@ -52,28 +51,28 @@ public static void main(String[] args) throws Exception {

private static class Task extends TimerTask {
private final LFXLightCollection lights;
private final List<Color> colors = Arrays.asList(
Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA,
Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW);
private final List<Number> colors = Arrays.asList(
(Number) 0xFF0000FF, (Number) 0xFF00FFFF, (Number) 0xFF00FF00, (Number) 0xFFFF00FF,
(Number) 0xFFFFA500, (Number) 0xFFFF69B4, (Number) 0xFFFF0000, (Number) 0xFFFFFF00);
private final Random random = new Random();
private volatile Color last;
private volatile Number last;

public Task(LFXLightCollection lights) {
this.lights = lights;
}

@Override
public void run() {
Color color = null;
Number color = null;
do {
color = colors.get(random.nextInt(colors.size()));
} while(color == last);
} while(color.equals(last));

for(LFXLight light: lights) {
if(!light.isPower()) {
light.setPower(true);
}
light.setColor(color);
light.setColor(color.intValue());
}
last = color;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.github.besherman.lifx.LFXClient;
import com.github.besherman.lifx.LFXLight;
import com.github.besherman.lifx.LFXLightCollection;
import java.awt.Color;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.github.besherman.lifx.LFXHSBKColor;
import com.github.besherman.lifx.LFXLight;
import com.github.besherman.lifx.LFXLightCollection;
import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
Expand Down Expand Up @@ -72,7 +71,7 @@ public void run() {
temp = MIN_TEMP;
}
System.out.println(temp);
LFXHSBKColor color = new LFXHSBKColor(Color.WHITE, temp);
LFXHSBKColor color = new LFXHSBKColor(0xFFFFFFFF, temp);
for(LFXLight light: lights) {
if(!light.isPower()) {
light.setPower(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.github.besherman.lifx.impl.entities.internal.structle.LxProtocolLight;
import com.github.besherman.lifx.impl.entities.internal.structle.StructleTypes;
import com.github.besherman.lifx.impl.network.LFXMessageRouter;
import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -158,12 +157,12 @@ public LFXHSBKColor getAverageColor() {
}

@Override
public void setColor(Color color) {
public void setColor(int color) {
setColor(new LFXHSBKColor(color));
}

@Override
public void setColor(Color color, long duration) {
public void setColor(int color, long duration) {
setColor(new LFXHSBKColor(color), duration);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.github.besherman.lifx.impl.entities.internal.structle.StructleTypes;
import com.github.besherman.lifx.impl.network.LFXMessageRouter;
import com.github.besherman.lifx.impl.network.LFXTimerQueue;
import java.awt.Color;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -133,12 +132,12 @@ public LFXHSBKColor getColor() {
}

@Override
public void setColor(Color color) {
public void setColor(int color) {
setColor(new LFXHSBKColor(color));
}

@Override
public void setColor(Color color, long duration) {
public void setColor(int color, long duration) {
setColor(new LFXHSBKColor(color), duration);
}

Expand Down

0 comments on commit 65f07fe

Please sign in to comment.