Class: OpenHAB::Core::Types::HSBType

Inherits:
PercentType show all
Defined in:
lib/openhab/core/types/hsb_type.rb

Overview

HSBType is a complex type with constituents for hue, saturation and brightness and can be used for color items.

Constant Summary collapse

BLACK =

Returns:

WHITE =

Returns:

RED =

Returns:

GREEN =

Returns:

BLUE =

Returns:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PercentType

#down?, #off?, #on?, #scale, #to_byte, #up?

Methods inherited from DecimalType

#-@, #coerce, #initialize, #|

Methods included from NumericType

#+@, #eql?, #to_d, #to_f, #to_i

Constructor Details

This class inherits a constructor from OpenHAB::Core::Types::DecimalType

Instance Attribute Details

#bluePercentType (readonly)

Returns:



# File 'lib/openhab/core/types/hsb_type.rb', line 179

#brightnessPercentType (readonly)

Returns:



# File 'lib/openhab/core/types/hsb_type.rb', line 170

#cctQuantityType (readonly)

Returns The correlated color temperature in Kelvin.

Returns:

  • (QuantityType)

    The correlated color temperature in Kelvin

See Also:

Since:

  • openHAB 4.3



194
195
196
# File 'lib/openhab/core/types/hsb_type.rb', line 194

def cct
  ColorUtil.xy_to_kelvin(to_xy[0..1].map { |x| x.double_value / 100 }) | "K"
end

#duvFloat (readonly)

Returns The delta u, v.

Returns:

  • (Float)

    The delta u, v

See Also:

Since:

  • openHAB 4.3



207
208
209
# File 'lib/openhab/core/types/hsb_type.rb', line 207

def duv
  ColorUtil.xy_to_duv(to_xy[0..1].map { |x| x.double_value / 100 })
end

#greenPercentType (readonly)

Returns:



# File 'lib/openhab/core/types/hsb_type.rb', line 176

#hueQuantityType (readonly)

Returns The color's hue component as a QuantityType of unit DEGREE_ANGLE.

Returns:



138
139
140
# File 'lib/openhab/core/types/hsb_type.rb', line 138

def hue
  QuantityType.new(raw_hue.to_big_decimal, Units::DEGREE_ANGLE)
end

#redPercentType (readonly)

Returns:



# File 'lib/openhab/core/types/hsb_type.rb', line 173

#saturationPercentType (readonly)

Returns:



# File 'lib/openhab/core/types/hsb_type.rb', line 167

Class Method Details

.from_cct(cct) ⇒ HSBType

Create HSBType from a color temperature

Parameters:

  • cct (QuantityType, Number)

    The color temperature (assumed in Kelvin, if not a QuantityType)

Returns:

Since:

  • openHAB 4.3



107
108
109
# File 'lib/openhab/core/types/hsb_type.rb', line 107

def from_cct(cct)
  from_xy(*ColorUtil.kelvin_to_xy((cct | "K").double_value))
end

.from_hsb(hue, saturation, brightness) ⇒ HSBType

Create HSBType from hue, saturation, and brightness values

Parameters:

Returns:



57
58
59
# File 'lib/openhab/core/types/hsb_type.rb', line 57

def from_hsb(hue, saturation, brightness)
  new(hue, saturation, brightness)
end

.from_rgb(r, g, b) ⇒ HSBType

Create HSBType from RGB values

Parameters:

  • r (Integer)

    Red component (0-255)

  • g (Integer)

    Green component (0-255)

  • b (Integer)

    Blue component (0-255)

Returns:



# File 'lib/openhab/core/types/hsb_type.rb', line 39

.from_xy(x, y) ⇒ HSBType

Create HSBType representing the provided xy color values in CIE XY color model

Parameters:

Returns:



# File 'lib/openhab/core/types/hsb_type.rb', line 46

Instance Method Details

#<=>(other) ⇒ Integer?

Comparison

Parameters:

Returns:

  • (Integer, nil)

    -1, 0, +1 depending on whether other is less than, equal to, or greater than self

    nil is returned if the two values are incomparable.



123
124
125
126
127
128
129
130
# File 'lib/openhab/core/types/hsb_type.rb', line 123

def <=>(other)
  logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" }
  if other.is_a?(HSBType)
    [brightness, hue, saturation] <=> [other.brightness, other.hue, other.saturation]
  else
    super
  end
end

#argbInteger

Convert to a packed 32-bit RGB value representing the color in the default sRGB color model.

The alpha component is always 100%.

Returns:



147
# File 'lib/openhab/core/types/hsb_type.rb', line 147

alias_method :argb, :rgb

#planckian?(duv_tolerance: 0.015, maximum_saturation: 75) ⇒ true, false Also known as: white_cct?

Note:

The parameters and defaults for this method are subject to change in future releases of this library, and should be considered beta. For now, the default parameters should be sufficient to detect most colors that Apple's HomeKit color temperature color chooser uses as planckian, without detecting most other "real" colors as planckian.

Checks if this color is within a certain tolerance of the planckian locus ("white")

Parameters:

  • duv_tolerance (Float) (defaults to: 0.015)

    The maximum allowed distance from the planckian locus

  • maximum_saturation (Numeric, PercentType) (defaults to: 75)

    The maximum allowed saturation. Some colors (bright green for example) may be close to the planckian locus, but you don't want to treat them as "white" because they are very saturated.

Returns:

  • (true, false)

See Also:

Since:

  • openHAB 4.3



228
229
230
# File 'lib/openhab/core/types/hsb_type.rb', line 228

def planckian?(duv_tolerance: 0.015, maximum_saturation: 75)
  duv.abs < duv_tolerance && saturation < maximum_saturation
end

#planckian_cct(range: nil, **kwargs) ⇒ QuantityType? Also known as: white_cct

Note:

Additional parameters are forwarded to #planckian?

Returns the color temperature of this color if it is within a certain tolerance of the planckian locus ("white"), otherwise returns nil.

Parameters:

  • range (Range, NumberItem) (defaults to: nil)

    An allowed range to additionally restrict if the CCT should be returned. A NumberItem that represents a CCT channel may be provided, and NumberItem#range will be used instead. If the range does not have units (is QuantityType), it is interpreted as being in Kelvin.

Returns:

  • (QuantityType, nil)

    The color temperature in Kelvin (unless the range is in mireds; then it will be in mireds)

See Also:

Since:

  • openHAB 4.3



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/openhab/core/types/hsb_type.rb', line 247

def planckian_cct(range: nil, **kwargs)
  return unless planckian?(**kwargs)

  range = range.range if range.is_a?(NumberItem)
  cct = self.cct
  if range
    range_type = range.begin || range.end
    if !range_type.is_a?(QuantityType)
      range = Range.new(range.begin | "K", range.end | "K")
    elsif range_type.unit == Units::MIRED
      cct |= Units::MIRED
    end
  end
  return nil if range && !range.cover?(cct)

  cct
end

#rgbInteger

Convert to a packed 24-bit RGB value representing the color in the default sRGB color model.

Returns:



151
152
153
# File 'lib/openhab/core/types/hsb_type.rb', line 151

def rgb
  argb & 0xffffff
end

#to_hexString

Convert to an HTML-style string of 6 hex characters in the default sRGB color model.

Returns:

  • (String)

    "#xxxxxx"



157
158
159
# File 'lib/openhab/core/types/hsb_type.rb', line 157

def to_hex
  Kernel.format("#%06x", rgb)
end

#to_rgb[PercentType, PercentType, PercentType]

Convert to RGB values representing the color in the default sRGB color model



# File 'lib/openhab/core/types/hsb_type.rb', line 182

#to_xy[PercentType, PercentType, PercentType]

Convert to the xyY values representing this object's color in CIE XY color model



# File 'lib/openhab/core/types/hsb_type.rb', line 186