gradientmaker v1.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #https://medium.com/@gratefulcheddar/rubygems-101-how-to-use-a-third-party-gem-in-your-ruby-application-37249529dbc7
  2. #knitpattern.rb FUNCTIONS WELL
  3. # This is a refactor
  4. require 'tty-prompt'
  5. require 'tty-screen'
  6. require 'pastel'
  7. require 'tty-cursor'
  8. require 'tty-font'
  9. # TITLE SCREEN #
  10. @pastel = Pastel.new
  11. @cursor = TTY::Cursor
  12. @font = TTY::Font.new(:doom)
  13. @reg_font = TTY::Font.new(:straight)
  14. def lines_at(lines, x, y)
  15. lines.each_with_index.reduce([]) do |acc, (line, i)|
  16. acc << @cursor.move_to(x - line.size/2, y - lines.size/2 + i) + line
  17. acc
  18. end
  19. end
  20. @size = TTY::Screen.size
  21. center_x = @size[1]/2
  22. center_y = @size[0]/2
  23. print @cursor.save
  24. print @cursor.clear_screen
  25. print @cursor.hide
  26. lines = @font.write('GRADIENT').split("\n")
  27. print @pastel.bright_red(lines_at(lines, center_x, center_y - 3).join)
  28. lines = @font.write('MAKER').split("\n")
  29. print @pastel.red(lines_at(lines, center_x, center_y + 3).join)
  30. print "\n\n"
  31. @cursor.next_line
  32. picarray = [
  33. ["□","□","□","□","□","□","□","□","□","□"],
  34. ["□","□","□","■","□","□","□","□","■","□"],
  35. ["□","□","■","□","■","□","□","■","□","■"],
  36. ["■","□","■","■","□","■","□","■","■","□"],
  37. ["■","□","■","■","■","■","□","■","■","■"],
  38. ["■","■","■","■","■","■","■","■","■","■"]
  39. ]
  40. #=begin
  41. picarray.each {|rw|
  42. rw.each {|cell|
  43. print cell
  44. }
  45. print "\n"
  46. }
  47. #=end
  48. print "press ENTER to continue"
  49. gets
  50. # INTRODUCTION #
  51. print "\n\n This program makes a grid of squares, gradually changing from one colour to another. You can use this pattern for cross stitch, knitting, or any other craft. The pattern will be a .csv file, which you can open in Excel or any other spreadsheet program."
  52. print "\n\n >> For row and stitch numbers, just type a number ('40' not '40 rows' or 'forty')."
  53. print "\n\n >> For Output File, type a word and do not use any punctuation. The file will overwrite any other files with this name in the folder, so doublecheck before you run the program. The finished file will be saved in the same folder as the gradientmaker.rb program".center(10)
  54. print "\n\n Answer each question, then press ENTER to continue. \n\n".center(10)
  55. # TAKE INPUTS #
  56. require "tty-prompt"
  57. prompt = TTY::Prompt.new
  58. checker = false
  59. until checker == true
  60. result = prompt.collect do
  61. key(:name).ask("1. Name your output file") do |q|
  62. q.modify :chomp
  63. q.required true
  64. q.validate(/[a-zA-Z]/)
  65. q.messages[:valid?] = "Please only use letters (lowercase and uppercase a-z and/or A-Z)"
  66. q.convert -> (input) { input << '.csv' }
  67. end
  68. print "\n"
  69. key(:row).ask("2. How many rows heigh is the gradient section?") do |q|
  70. q.convert :int
  71. q.messages[:convert?] = "That isn't a number. Please type in a number in digits, i.e. '30', with no words or punctuation"
  72. q.modify :chomp
  73. q.required true
  74. end
  75. print "\n"
  76. key(:stitch).ask("3. How many stitches per row wide is the gradient section?") do |q|
  77. q.convert :int
  78. q.messages[:convert?] = "That isn't a number. Please type in a number in digits, i.e. '30', with no words or punctuation"
  79. q.modify :chomp
  80. q.required true
  81. end
  82. end
  83. print "\n \n"
  84. print "You are about to create a new file called #{result[:name]}, containing a pattern #{result[:row]} rows heigh and #{result[:stitch]} stitches wide."
  85. print "\n \n"
  86. checker = prompt.yes?("Is this correct?")
  87. end
  88. =begin
  89. 1.upto(100) do |i|
  90. printf("\rProcessing: %d%%", i)
  91. sleep(0.05)
  92. end
  93. =end
  94. print "\n \n [ COMPLETE! ]"
  95. print "\n\n Open your new file ( #{result[:name]}) and see if the pattern is correct! Then save it in a different format (.xls, .ods etc) to add conditional formatting (which will automatically adds the two different colour backgrounds to the cells) change the column and row heights to look square, and add any more details. If it doesn't look quite right, then run the program again with new numbers."
  96. print "\n\n"
  97. print "\n\n"
  98. # Make an array to hold the pattern
  99. pattern = Array.new(result[:row]) {Array.new}
  100. # Calculate the pattern
  101. pattern.each_index {|i|
  102. #puts "ROW #{i}/#{pattern.count}"
  103. #what percentage of the way through the rows are we?
  104. rw = i + 1
  105. perc = rw.to_f/result[:row].to_f
  106. #this is also the percentage of stitches that are black on this row;
  107. # i.e. 25% down the gradient, 25% of the stitches are black.
  108. # 100% down the gradient, 100% of the stitches are black
  109. # if perc goes above 1, or is a negative number, there will be an error
  110. if perc > 1.0 or perc < 0.0
  111. puts "ERROR: perc is #{perc}"
  112. gets
  113. end
  114. #puts "perc is #{perc}, a #{perc.class}"
  115. #gets
  116. black = result[:stitch] * perc
  117. black = black.round #stitches must be a whole number
  118. #puts "black = #{black}"
  119. white = result[:stitch] - black
  120. #puts "white = #{white}"
  121. pattern[i] << '■' * black
  122. pattern[i] << '□' * white
  123. }
  124. # join into a single string, i guess
  125. pattern.each {|rw|
  126. rw[0] << rw[1]
  127. rw.delete_at(1)
  128. }
  129. # shuffle time, and deal with the doublearray issue created
  130. def string_shuffle(s)
  131. s.split("").shuffle!
  132. end
  133. pattern.each {|rw|
  134. rw[0] = string_shuffle(rw[0])
  135. rw.flatten!
  136. }
  137. # OPTIONALLY
  138. # Add a Row Number to the beginning of each row
  139. # Add a Row at the top counting stitch numbers for you also
  140. # write to csv
  141. require "csv"
  142. File.write(result[:name], pattern.map(&:to_csv).join)
  143. #TEST
  144. ##A square YES
  145. ##Rectangle: longer than wide ALL YES
  146. # both numbers even
  147. # both numbers odd
  148. # A odd, B even
  149. # A even, B odd
  150. ##Rectangle: wider than long ALL YES
  151. # both numbers even
  152. # both numbers odd
  153. # A odd, B even
  154. # A even, B odd
  155. # Catch wrong number inputs
  156. # NOW OPEN IN EXCEL - ADD CONDITIONAL FORMATTING
  157. # 0. highlight and copy the white square
  158. # 1. highlight whole document
  159. # 2. go to conditional formatting
  160. # 3. paste white square, and choose a colour
  161. # 4. document will now show colours graphically
  162. # 5. repeat with black square
  163. # 6. save file as a different format (NOT a .csv)