class CTioga2::MetaBuilder::Types::TiogaColorType

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.

Constants

HLSRegexp

Public Instance Methods

parse_one_color(str) click to toggle source
# 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) click to toggle source
# 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