Class: CTioga2::MetaBuilder::Types::TiogaColorType

Inherits:
CTioga2::MetaBuilder::Type show all
Defined in:
lib/ctioga2/metabuilder/types/styles.rb

Overview

A color for use with Tioga, ie a [red, green, blue] array of values between 0 and 1.0. It accepts HTML-like colors, or three comma-separated values between 0 and 1.

Constant Summary collapse

HLSRegexp =
/(?:hls):/i

Instance Attribute Summary

Attributes inherited from CTioga2::MetaBuilder::Type

#namespace, #namespace_lookup, #passthrough, #re_shortcuts, #shortcuts, #type

Instance Method Summary collapse

Methods inherited from CTioga2::MetaBuilder::Type

#boolean?, #default_value, from_string, get_param_type, get_type, #initialize, #option_parser_long_option, #option_parser_option, #string_to_type, #type_name, type_name, #type_to_string

Constructor Details

This class inherits a constructor from CTioga2::MetaBuilder::Type

Instance Method Details

#parse_one_color(str) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ctioga2/metabuilder/types/styles.rb', line 35

def parse_one_color(str)
  if str =~ HLSRegexp
    hls = true
    str = str.gsub(HLSRegexp,'')
  else
    hls = false
  end
  if str =~ /^\s*#([0-9a-fA-F]{6})\s*$/
    value =  $1.scan(/../).map {
      |i| i.to_i(16)/255.0 
    }
  elsif str =~ /^\s*#([0-9a-fA-F]{3})\s*$/
    value =  $1.scan(/./).map {
      |i| i.to_i(16)/15.0 
    }
  else
    begin 
      if Tioga::ColorConstants::const_defined?(str)
        value = Tioga::ColorConstants::const_get(str)
        return value
      end
    rescue
    end
    value = str.split(/\s*,\s*/).map do |s|
      s.to_f
    end
  end
  if value.size != 3
    raise IncorrectInput, "You need exactly three values to make up a color"
  end
  if hls
    # Requires Tioga r599
    value = Tioga::FigureMaker.hls_to_rgb(value)
  end
  return value
end

#string_to_type_internal(str) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ctioga2/metabuilder/types/styles.rb', line 72

def string_to_type_internal(str)
  # We implement a xcolor-like color mix stuff
  elems = str.split(/!(\d+(?:\.\d+)?)!?/)
  if (elems.size % 2) == 0
    elems << "White"    # Implicit mix with white
  end

  temp = parse_one_color(elems.shift).dup
  
  while elems.size > 0
    frac = elems.shift.to_f/100.0
    new_color = parse_one_color(elems.shift)
    3.times do |i|
      temp[i] = frac * temp[i] + (1 - frac) * new_color[i]
    end
  end

  return temp
end