Class: Artsy

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

Overview

artsy.rb ###

Make your Ruby terminal programs look nice!        ###

Developed by Brett (github.com/notronaldmcdonald) ###

Defined Under Namespace

Classes: Handler

Class Method Summary collapse

Class Method Details

.gradientOut(text, r1, g1, b1, r2, g2, b2, stepping, debug = 0) ⇒ Object



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
# File 'lib/artsy.rb', line 93

def self.gradientOut(text, r1, g1, b1, r2, g2, b2, stepping, debug = 0)
  # before doing anything, check both rgb sets
  Handler.rgbOutOfRange("strict", r1, g1, b1)
  Handler.rgbOutOfRange("strict", r2, g2, b2)
  # prints text with a gradient
  # increases every value by stepping per character
  arr_Text = text.split("") # split text into array of characters
  if debug == 1
    # show array
    puts arr_Text
  end
  count = arr_Text.count
  if debug == 1
    puts count
  end
  count_cap = count - 1
  cr = r1
  cg = g1
  cb = b1
  for i in 0..count_cap
    # iterate over characters
    if r1 < r2
      # if less than r2, +step and check nr accordingly
      nr = cr + stepping
      if nr < r2
        # as long as less than r2, increment by stepping
        cr = nr # cr is now nr
      end
    else
      # -step and check nr differently
      nr = cr - stepping
      if nr > r2
        cr = nr
      end
    end
    if g1 < g2
      ng = cg + stepping
      if ng < g2
        # green
        cg = ng
      end
    else
      ng = cg - stepping
      if ng > g2
        cg = ng
      end
    end
    if b1 < b2
      nb = cb + stepping
      if nb < b2
        cb = nb
      end
    else
      nb = cb - stepping
      if nb > b2
        cb = nb
      end
    end
    # in conclusion, don't increase if next color exceeds endpoint
    print "\033[38;2;#{cr};#{cg};#{cb}m#{arr_Text[i]}\033[0m"
  end
  print "\n" # print a newline
end

.out(text, colortype, r = 0, g = 0, b = 0, color: "none", formatting: "none") ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/artsy.rb', line 47

def self.out(text, colortype, r = 0, g = 0, b = 0, color: "none", formatting: "none")
  definedColours = ["\033[0m", "\033[31m", "\033[32m", "\033[93m", "\033[91m", "\033[92m", "\033[96m"]
  # print colored/formatted text (either from the preset list or with truecolors)
  # you MUST specify if you're printing truecolor or not
  if colortype == "normal"
    # print with regular colors
    if color == "none"
      # fail if color undefined
      raise ArgumentError.new "Invalid color defined (#{color})."
    elsif color == "red"
      # printcolor = red
      printcolor = "#{definedColours[1]}"
    elsif color == "green"
      printcolor = "#{definedColours[2]}"
    elsif color == "yellow"
      printcolor = "#{definedColours[3]}"
    elsif color == "failureRed"
      printcolor = "#{definedColours[4]}"
    elsif color == "successGreen"
      printcolor = "#{definedColours[5]}"
    elsif color == "cyan"
      printcolor = "#{definedColours[6]}"
    else
      # fail
      raise ArgumentError.new "Invalid color defined (#{color})."
    end
  elsif colortype == "true"
    # before doing anything, ensure all rgb values are within range
    Handler.rgbOutOfRange("strict", r, g, b) # calls the handler check and passes rgb
    # print with rgb
    printcolor = "\033[38;2;#{r};#{g};#{b}m"
  else
    # fail
    raise ArgumentError.new "Invalid colortype defined (#{colortype})."
  end
  if formatting == "none"
    puts "#{printcolor}#{text}#{definedColours[0]}" # done!
  elsif formatting == "bold"
    puts "#{printcolor}\033[1m#{text}#{definedColours[0]}"
  elsif formatting == "underline"
    puts "#{printcolor}\033[4m#{text}#{definedColours[0]}"
  elsif formatting == "italics"
    # WARNING: Not widely supported by terminal emulators.
    puts "#{printcolor}\033[3m#{text}#{definedColours[0]}"
  end
end