Class: Pxlsrt::Helpers

Inherits:
Object
  • Object
show all
Defined in:
lib/pxlsrt/helpers.rb

Overview

Methods not having to do with image or color manipulation.

Class Method Summary collapse

Class Method Details

.checkOptions(options, rules) ⇒ Object

Checks if supplied options follow the rules.



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
# File 'lib/pxlsrt/helpers.rb', line 37

def self.checkOptions(options, rules)
  match=true
  for o in options.keys
    o_match=false
    if rules[o].class==Array
      if rules[o].include?(options[o])
        o_match=true
      else
        for r in 0...rules[o].length
          if rules[o][r].class==Hash
            for n in rules[o][r][:class]
              if n==options[o].class
                o_match=match
                break
              end
            end
          end
          if o_match==true
            break
          end
        end
      end
    elsif rules[o] == :anything
      o_match = true
    end
    match=(match and o_match)
    if match==false
      break
    end
  end
  return match
end

.contented(c) ⇒ Object

Determines if a value has content.



7
8
9
# File 'lib/pxlsrt/helpers.rb', line 7

def self.contented(c)
  return c != nil
end

.cyan(what) ⇒ Object

Used to output a cyan string to the terminal.



17
18
19
# File 'lib/pxlsrt/helpers.rb', line 17

def self.cyan(what)
  return "\e[36m#{what}\e[0m"
end

.error(what) ⇒ Object

Prints an error message.



95
96
97
# File 'lib/pxlsrt/helpers.rb', line 95

def self.error(what)
  puts "#{Pxlsrt::Helpers.red("pxlsrt")} #{what}"
end

.green(what) ⇒ Object

Used to output a green string to the terminal.



27
28
29
# File 'lib/pxlsrt/helpers.rb', line 27

def self.green(what)
  return "\e[32m#{what}\e[0m"
end

.handlePixelSort(band, o) ⇒ Object

Pixel sorting helper to eliminate repetition.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pxlsrt/helpers.rb', line 71

def self.handlePixelSort(band, o)
  if (o[:reverse].class == String and (o[:reverse].downcase == "reverse" or o[:reverse] == "")) or o[:reverse] == true
    reverse = 1
  elsif o[:reverse].class == String and o[:reverse].downcase == "either"
    reverse = -1
  else
    reverse = 0
  end
  if o[:smooth]
    u = band.group_by { |x| x }
    k = u.keys
  else
    k = band
  end
  sortedBand = Pxlsrt::Colors.pixelSort(
    k,
    o[:method],
    reverse
  )
  sortedBand = sortedBand.map { |x| u[x] }.flatten(1) if o[:smooth]
  return Pxlsrt::Lines.handleMiddlate(sortedBand, o[:middle])
end

.isNumeric?(s) ⇒ Boolean

Determines if a string can be a float or integer.

Returns:

  • (Boolean)


32
33
34
# File 'lib/pxlsrt/helpers.rb', line 32

def self.isNumeric?(s)
  true if Float(s) rescue false
end

.progress(what, amount, outof) ⇒ Object

Progress indication.



105
106
107
108
109
110
111
112
113
# File 'lib/pxlsrt/helpers.rb', line 105

def self.progress(what, amount, outof)
  progress = (amount.to_f * 100.0 / outof.to_f).to_i
  if progress == 100
    puts "\r#{Pxlsrt::Helpers.green("pxlsrt")} #{what} (#{Pxlsrt::Helpers.green("#{progress}%")})"
  else
    $stdout.write "\r#{Pxlsrt::Helpers.yellow("pxlsrt")} #{what} (#{Pxlsrt::Helpers.yellow("#{progress}%")})"
    $stdout.flush
  end
end

.red(what) ⇒ Object

Used to output a red string to the terminal.



12
13
14
# File 'lib/pxlsrt/helpers.rb', line 12

def self.red(what)
  return "\e[31m#{what}\e[0m"
end

.verbose(what) ⇒ Object

Prints something.



100
101
102
# File 'lib/pxlsrt/helpers.rb', line 100

def self.verbose(what)
  puts "#{Pxlsrt::Helpers.cyan("pxlsrt")} #{what}"
end

.yellow(what) ⇒ Object

Used to output a yellow string to the terminal.



22
23
24
# File 'lib/pxlsrt/helpers.rb', line 22

def self.yellow(what)
  return "\e[33m#{what}\e[0m"
end