-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use color picker to set color of smart bulbs
- Loading branch information
1 parent
547ed28
commit 712ed8c
Showing
9 changed files
with
304 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,72 @@ | |
* Authored by: Marius Meisenzahl <[email protected]> | ||
*/ | ||
|
||
public class Colors.HSB {} | ||
public class Colors.HSB { | ||
public uint16 hue; | ||
public uint8 saturation; | ||
public uint8 brightness; | ||
|
||
public HSB () {} | ||
|
||
public HSB.from_rgb (RGB rgb) { | ||
double min, max, delta; | ||
double h, s, b; | ||
|
||
double red = rgb.red / 255.0; | ||
double green = rgb.green / 255.0; | ||
double blue = rgb.blue / 255.0; | ||
|
||
min = red; | ||
min = green < min ? green : min; | ||
min = blue < min ? blue : min; | ||
|
||
max = red; | ||
max = green > max ? green : max; | ||
max = blue > max ? blue : max; | ||
|
||
b = max; | ||
delta = max - min; | ||
|
||
if (max != 0) { | ||
s = delta / max; | ||
} else { | ||
s = 0; | ||
h = 0; | ||
|
||
hue = (uint16) (h + 0.5); | ||
saturation = (uint8) (s * 100 + 0.5); | ||
brightness = (uint8) (b * 100 + 0.5); | ||
|
||
return; | ||
} | ||
|
||
if (max == min) { | ||
h = 0; | ||
s = 0; | ||
|
||
hue = (uint16) (h + 0.5); | ||
saturation = (uint8) (s * 100 + 0.5); | ||
brightness = (uint8) (b * 100 + 0.5); | ||
|
||
return; | ||
} | ||
|
||
if (red == max) { | ||
h = (green - blue) / delta; | ||
} else if (green == max) { | ||
h = 2 + (blue - red) / delta; | ||
} else { | ||
h = 4 + (red - green) / delta; | ||
} | ||
|
||
h *= 60; | ||
|
||
if (h < 0) { | ||
h += 360; | ||
} | ||
|
||
hue = (uint16) (h + 0.5); | ||
saturation = (uint8) (s * 100 + 0.5); | ||
brightness = (uint8) (b * 100 + 0.5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
public class Widgets.ColorPicker: Gtk.DrawingArea { | ||
private Gdk.RGBA color; | ||
private Gtk.Window window; | ||
private bool dialog_visible; | ||
|
||
public signal void on_color_change (Colors.RGB color); | ||
|
||
public ColorPicker (Gtk.Window window) { | ||
this.window = window; | ||
color.parse ("#4caf50"); | ||
|
||
set_size_request (140, 140); | ||
add_events (Gdk.EventMask.ALL_EVENTS_MASK); | ||
|
||
button_press_event.connect ((event) => { | ||
if (event.button == 1 && !dialog_visible) { | ||
var dialog = new Gtk.ColorSelectionDialog (""); | ||
unowned Gtk.ColorSelection widget = dialog.get_color_selection (); | ||
|
||
widget.current_rgba = color; | ||
|
||
dialog.deletable = false; | ||
dialog.transient_for = window; | ||
dialog_visible = true; | ||
|
||
if (dialog.run () == Gtk.ResponseType.OK) { | ||
if (color != widget.current_rgba) { | ||
color = widget.current_rgba; | ||
|
||
var rgb = new Colors.RGB (); | ||
rgb.red = (uint8) (color.red * 255 + 0.5); | ||
rgb.green = (uint8) (color.green * 255 + 0.5); | ||
rgb.blue = (uint8) (color.blue * 255 + 0.5); | ||
|
||
on_color_change (rgb); | ||
} | ||
} | ||
|
||
dialog_visible = false; | ||
dialog.close (); | ||
} | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
public override bool draw (Cairo.Context ctx) { | ||
int width = get_allocated_width (); | ||
int height = get_allocated_height (); | ||
|
||
// Draw an arc: | ||
double xc = width / 2.0; | ||
double yc = height / 2.0; | ||
double radius = (int.min (width, height) / 2.0); | ||
double angle1 = 0; | ||
double angle2 = 2 * Math.PI; | ||
|
||
int shadow_width = 6; | ||
double shadow_alpha = 1.0; | ||
string shadow_color = "#A9A9A9"; | ||
for (int i = 1; i <= shadow_width; i++) { | ||
ctx.arc (xc, yc, radius - i, angle1, angle2); | ||
Gdk.RGBA c = Gdk.RGBA(); | ||
c.parse (shadow_color); | ||
c.alpha = shadow_alpha / ((shadow_width - i + 1) * (shadow_width - i + 1)); | ||
Gdk.cairo_set_source_rgba (ctx, c); | ||
ctx.stroke (); | ||
} | ||
|
||
ctx.arc (xc, yc, radius - shadow_width, angle1, angle2); | ||
Gdk.cairo_set_source_rgba (ctx, color); | ||
ctx.fill (); | ||
|
||
return true; | ||
} | ||
|
||
public Colors.RGB rgb { | ||
set { | ||
color.parse (value.to_hex ()); | ||
} | ||
} | ||
|
||
public Colors.HSB hsb { | ||
set { | ||
color.parse (new Colors.RGB.from_hsb (value).to_hex ()); | ||
} | ||
} | ||
} |