mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-16 13:56:35 +00:00
Use hs color when outside of wheel (#9088)
This commit is contained in:
parent
2c08cba8cc
commit
105a00d3e4
@ -110,5 +110,19 @@ export const rgb2hsv = (
|
||||
return [60 * (h < 0 ? h + 6 : h), v && c / v, v];
|
||||
};
|
||||
|
||||
export const hsv2rgb = (
|
||||
hsv: [number, number, number]
|
||||
): [number, number, number] => {
|
||||
const [h, s, v] = hsv;
|
||||
const f = (n: number) => {
|
||||
const k = (n + h / 60) % 6;
|
||||
return v - v * s * Math.max(Math.min(k, 4 - k, 1), 0);
|
||||
};
|
||||
return [f(5), f(3), f(1)];
|
||||
};
|
||||
|
||||
export const rgb2hs = (rgb: [number, number, number]): [number, number] =>
|
||||
rgb2hsv(rgb).slice(0, 2) as [number, number];
|
||||
|
||||
export const hs2rgb = (hs: [number, number]): [number, number, number] =>
|
||||
hsv2rgb([hs[0], hs[1], 255]);
|
||||
|
@ -2,7 +2,7 @@ import { html } from "@polymer/polymer/lib/utils/html-tag";
|
||||
/* eslint-plugin-disable lit */
|
||||
import { PolymerElement } from "@polymer/polymer/polymer-element";
|
||||
import { EventsMixin } from "../mixins/events-mixin";
|
||||
import { rgb2hs } from "../common/color/convert-color";
|
||||
import { hs2rgb, rgb2hs } from "../common/color/convert-color";
|
||||
/**
|
||||
* Color-picker custom element
|
||||
*
|
||||
@ -291,7 +291,13 @@ class HaColorPicker extends EventsMixin(PolymerElement) {
|
||||
processUserSelect(ev) {
|
||||
const canvasXY = this.convertToCanvasCoordinates(ev.clientX, ev.clientY);
|
||||
const hs = this.getColor(canvasXY.x, canvasXY.y);
|
||||
const rgb = this.getRgbColor(canvasXY.x, canvasXY.y);
|
||||
let rgb;
|
||||
if (!this.isInWheel(canvasXY.x, canvasXY.y)) {
|
||||
const [r, g, b] = hs2rgb([hs.h, hs.s]);
|
||||
rgb = { r, g, b };
|
||||
} else {
|
||||
rgb = this.getRgbColor(canvasXY.x, canvasXY.y);
|
||||
}
|
||||
this.onColorSelect(hs, rgb);
|
||||
}
|
||||
|
||||
|
@ -254,6 +254,7 @@ class MoreInfoLight extends LitElement {
|
||||
}
|
||||
|
||||
let brightnessAdjust = 100;
|
||||
this._brightnessAdjusted = undefined;
|
||||
if (
|
||||
stateObj.attributes.color_mode === LightColorModes.RGB &&
|
||||
!lightSupportsColorMode(stateObj, LightColorModes.RGBWW) &&
|
||||
@ -264,8 +265,6 @@ class MoreInfoLight extends LitElement {
|
||||
this._brightnessAdjusted = maxVal;
|
||||
brightnessAdjust = (this._brightnessAdjusted / 255) * 100;
|
||||
}
|
||||
} else {
|
||||
this._brightnessAdjusted = undefined;
|
||||
}
|
||||
this._brightnessSliderValue = Math.round(
|
||||
(stateObj.attributes.brightness * brightnessAdjust) / 255
|
||||
@ -415,7 +414,7 @@ class MoreInfoLight extends LitElement {
|
||||
255,
|
||||
]) as [number, number, number];
|
||||
|
||||
this._setRgbColor(
|
||||
this._setRgbWColor(
|
||||
this._adjustColorBrightness(
|
||||
// first normalize the value
|
||||
this._colorBrightnessSliderValue
|
||||
@ -457,7 +456,7 @@ class MoreInfoLight extends LitElement {
|
||||
return rgbColor;
|
||||
}
|
||||
|
||||
private _setRgbColor(rgbColor: [number, number, number]) {
|
||||
private _setRgbWColor(rgbColor: [number, number, number]) {
|
||||
if (lightSupportsColorMode(this.stateObj!, LightColorModes.RGBWW)) {
|
||||
const rgbww_color: [number, number, number, number, number] = this
|
||||
.stateObj!.attributes.rgbww_color
|
||||
@ -483,12 +482,17 @@ class MoreInfoLight extends LitElement {
|
||||
* Called when a new color has been picked.
|
||||
* should be throttled with the 'throttle=' attribute of the color picker
|
||||
*/
|
||||
private _colorPicked(ev: CustomEvent) {
|
||||
private _colorPicked(
|
||||
ev: CustomEvent<{
|
||||
hs: { h: number; s: number };
|
||||
rgb: { r: number; g: number; b: number };
|
||||
}>
|
||||
) {
|
||||
if (
|
||||
lightSupportsColorMode(this.stateObj!, LightColorModes.RGBWW) ||
|
||||
lightSupportsColorMode(this.stateObj!, LightColorModes.RGBW)
|
||||
) {
|
||||
this._setRgbColor(
|
||||
this._setRgbWColor(
|
||||
this._colorBrightnessSliderValue
|
||||
? this._adjustColorBrightness(
|
||||
[ev.detail.rgb.r, ev.detail.rgb.g, ev.detail.rgb.b],
|
||||
@ -497,10 +501,10 @@ class MoreInfoLight extends LitElement {
|
||||
: [ev.detail.rgb.r, ev.detail.rgb.g, ev.detail.rgb.b]
|
||||
);
|
||||
} else if (lightSupportsColorMode(this.stateObj!, LightColorModes.RGB)) {
|
||||
const rgb_color = [ev.detail.rgb.r, ev.detail.rgb.g, ev.detail.rgb.b] as [
|
||||
number,
|
||||
number,
|
||||
number
|
||||
const rgb_color: [number, number, number] = [
|
||||
ev.detail.rgb.r,
|
||||
ev.detail.rgb.g,
|
||||
ev.detail.rgb.b,
|
||||
];
|
||||
if (this._brightnessAdjusted) {
|
||||
this.hass.callService("light", "turn_on", {
|
||||
|
Loading…
x
Reference in New Issue
Block a user