Class: Coloryze

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/color/coloryze.rb

Constant Summary collapse

VERSION =
"2.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, outio = $stdout, errio = $stderr) ⇒ Coloryze

Returns a new instance of Coloryze.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/color/coloryze.rb', line 62

def initialize(args, outio = $stdout, errio = $stderr)
  info "args: #{args}"

  @exit_status = 0
  @scheme_options = Array.new

  _add_option("monochromatic",        "display one hue",                                           Color::Monochromatic,       :hue)
  _add_option("split-complementary",  "display hue and two complements near 180 degrees",          Color::SplitComplementary,  :hue, :deg)
  _add_option("double-complementary", "display two hues and their two complements",                Color::DoubleComplementary, :hue, :hue)
  _add_option("complementary",        "display hues at 180 degrees from each other",               Color::Complementary,       :hue)
  _add_option("analogous",            "display three hues near each other",                        Color::Analogous,           :hue, :deg)
  _add_option("triadic",              "display three equidistant hues",                            Color::Triadic,             :hue)
  _add_option("hues",                 "display colors for the generated hues (no scheme applied)", Color::Hues,                :start, :end, :int)

  output_type = :html
  sample = nil
  total_sample = nil
  
  @scheme_options.each do |sopt|
    # info "sopt: #{sopt}"
  end
  
  sopt = nil
  scheme_args = Array.new

  params = {
    :background       => nil,
    :saturation_start => nil,
    :saturation_end   => nil,
    :saturation_step  => nil,
    :brightness_start => nil,
    :brightness_end   => nil,
    :brightness_step  => nil,
  }

  args.each do |arg|
    info "arg: #{arg}"

    case arg
    when %r{--(saturation|brightness)=(\d+),(\d+),(\d+)}
      md = Regexp.last_match
      fieldname = md[1]
      %w{ _start _end _step }.each_with_index do |fieldsuffix, idx|
        field = (fieldname + fieldsuffix).intern
        params[field] = md[idx + 2].to_i
      end
      # not supported
      #       when %r{--background}
      #         args[:background] = md[1]
    when %r{--output=(\w+)}
      md = Regexp.last_match
      output_type = md[1].intern
    when "--version"
      show_version(outio)
      @exit_status = 0
      return
    when "--verbose"
      Log.level = Log::DEBUG
    when %r{--sample=(\d+)}
      md = Regexp.last_match
      sample = md[1].to_i
    when %r{--total-sample=(\d+)}
      md = Regexp.last_match
      total_sample = md[1].to_i
    when "--help", "--usage"
      show_usage(outio)
      @exit_status = 0
      return
    else
      md = nil
      if sopt
        $stderr.puts "error: invalid argument: #{arg}"
        @sxit_status = 1
      elsif sopt = @scheme_options.detect { |so| md = so.re.match(arg) }
        sopt.set_args(md[1 .. -1])
        scheme_args.concat(md[1 .. -1])

        # add process for scheme without the right parameters
      end
    end
  end

  unless sopt
    # check for invalid scheme parameters

    args.each do |arg|
      @scheme_options.each do |so|
        if arg.index("--" + so.stype)
          info "matched #{so}"
          errio.puts "error: scheme '#{so.stype}' requires " + so.params.collect { |p| p.to_s }.join(",")
          @exit_status = 1
          return
        end
      end
    end
  end

  unless sopt
    show_usage(outio)
    @exit_status = 1
    return
  end

  info "sopt: #{sopt}"
  info "scheme_args: #{scheme_args}"

  hues = sopt.cls.new(*sopt.args).hues

  info "hues: #{hues}"
  info "params: #{params}"

  info "sample: #{sample}"
  info "total_sample: #{total_sample}"

  palette_cls = if sample || total_sample
                  # yes, a mismatch between *sample and *samples ... legacy option.
                  params[:samples] = sample
                  params[:total_samples] = total_sample
                  Color::SamplePalette
                else
                  Color::FullPalette
                end
  
  palette = palette_cls.new(hues, params)
  info "palette: #{palette}"
  if output_type == :html
    palette.print_as_html(outio)
  else
    palette.print_as_text(outio)
  end
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



60
61
62
# File 'lib/color/coloryze.rb', line 60

def exit_status
  @exit_status
end

Instance Method Details

#_add_option(stype, desc, cls, *params) ⇒ Object



221
222
223
# File 'lib/color/coloryze.rb', line 221

def _add_option(stype, desc, cls, *params)
  @scheme_options << SchemeOption.new(stype, desc, cls, *params)
end

#show_usage(io) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/color/coloryze.rb', line 200

def show_usage(io)
  io.puts "usage: coloryze [options]"
  io.puts "options:"
  options = Array.new
  options << [ "--help, --usage",                "display this help message" ]
  @scheme_options.each do |sopt|
    options << [ sopt.option, sopt.desc ]
  end
  options << [ "--sample=NUM",                   "select only NUM colors per hue, chosen randomly" ]
  options << [ "--total-sample=NUM",             "select only NUM total colors, chosen randomly" ]
  options << [ "--saturation=START,END,INT",     "set the saturation start, end, and interval" ]
  options << [ "--brightness=START,END,INT",     "set the brightness start, end, and interval" ]
  options << [ "--output=TYPE",                  "set the output type, \"text\" (the default) or \"html\"" ]
  options << [ "--verbose",                      "produce debugging output" ]
  options << [ "--version",                      "display the version and exit"]

  options.each do |opt|
    io.printf "    %-30s  %s\n", *opt
  end
end

#show_version(io) ⇒ Object



194
195
196
197
198
# File 'lib/color/coloryze.rb', line 194

def show_version(io)
  io.puts "coloryze, version #{VERSION}"
  io.puts "Written by Jeff Pace ([email protected])"
  io.puts "Released under the GNU Public License."
end