class Artsy

                  artsy.rb                         ###

Make your Ruby terminal programs look nice!        ###

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

Licensed under the GNU LGPLv3 license. See the LICENSE in ###

the project root for more information.          ###

Public Class Methods

gradientOut(text, r1, g1, b1, r2, g2, b2, stepping, debug = 0) click to toggle source
# File lib/artsy.rb, line 63
def self.gradientOut(text, r1, g1, b1, r2, g2, b2, stepping, debug = 0)
  # before doing anything, check both rgb sets
  ArtsyDev::Handler.rgbOutOfRange("strict", r1, g1, b1)
  ArtsyDev::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") click to toggle source
# File lib/artsy.rb, line 14
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
    ArtsyDev::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
  case formatting
    when "none"
      puts "#{printcolor}#{text}#{definedColours[0]}" # done!
    when "bold"
      puts "#{printcolor}\033[1m#{text}#{definedColours[0]}"
    when "underline", "ul"
      puts "#{printcolor}\033[4m#{text}#{definedColours[0]}"
    when "italics"
      # WARNING: Not widely supported by terminal emulators.
      puts "#{printcolor}\033[3m#{text}#{definedColours[0]}"
    else
      raise ArgumentError.new "Invalid formatting option set (#{formatting})."
  end
end