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 181

#brightnessPercentType (readonly)

Returns:



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

#cctQuantityType (readonly)

Returns The correlated color temperature in Kelvin.

Returns:

  • (QuantityType)

    The correlated color temperature in Kelvin

See Also:

Since:

  • openHAB 4.3



196
197
198
# File 'lib/openhab/core/types/hsb_type.rb', line 196

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



209
210
211
# File 'lib/openhab/core/types/hsb_type.rb', line 209

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 178

#hueQuantityType (readonly)

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

Returns:



140
141
142
# File 'lib/openhab/core/types/hsb_type.rb', line 140

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 175

#saturationPercentType (readonly)

Returns:



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

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



109
110
111
# File 'lib/openhab/core/types/hsb_type.rb', line 109

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:



59
60
61
# File 'lib/openhab/core/types/hsb_type.rb', line 59

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 41

.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 48

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.



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

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:



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

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



230
231
232
# File 'lib/openhab/core/types/hsb_type.rb', line 230

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



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

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.to_s == "mired"
      cct |= "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:



153
154
155
# File 'lib/openhab/core/types/hsb_type.rb', line 153

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"



159
160
161
# File 'lib/openhab/core/types/hsb_type.rb', line 159

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 184

#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 188