require 'chunkypng' class Color def initialize(colortable=nil) @colortable = colortable…
- User
require 'chunky_png' class Color def initialize(color_table=nil) @color_table = color_table || [1, 0] end
def rgb colors = [ [[255,192,192], [255,0,0], [192,0,0]], # Red [[255,255,192], [255,255,0], [192,192,0]], # Yellow [[192,255,192], [0,255,0], [0,192,0]], # Green [[192,255,255], [0,255,255], [0,192,192]], # Cyan [[192,192,255], [0,0,255], [0,0,192]], # Blue [[255,192,255], [255,0,255], [192,0,192]], # Magenta ] colors[@color_table[1]][@color_table[0]] end
def push_color @color_table[0] = (@color_table[0] + 1) % 3 rgb end
def write_color @color_table[0] = (@color_table[0] + 2) % 3 @color_table[1] = (@color_table[1] + 5) % 6 rgb end end
current_color = Color.new piet_painting = []
def draw_block(piet_painting, current_color,size,num) block = Array.new(12) { Array.new(12) { Array.new(3, 0) } } if num != 0 old_push_color = current_color.push_color current_color.write_color
block.each_index do |i| block[i].each_index do |j| block[i][j] = current_color.rgb end end block[0][0] = old_push_color size += 1else block.each_index do |i| block[i].each_index do |j| block[i][j] = current_color.rgb end end end
pix_lft = 144 - size div = pix_lft / 12 rem = pix_lft % 12
if div != 0 block[(12-div)..-1].each_index do |i| block[(12-div)..-1][i].each_index do |j| block[(12-div)+i][j] = [0,0,0] end end end block[(11-div)..-1].each_index do |i| block[(11-div)..-1][i][0...rem].each_index do |j| block[(11-div)+i][j] = [0,0,0] end end pos_y = 12 * num pos_x = 0 piet_painting[pos_x...(pos_x+12)].each_index do |i| piet_painting[pos_x...(pos_x+12)][i][pos_y...(pos_y+12)].each_index do |j| piet_painting[pos_x+i][pos_y+j] = block[i][j] end end end
def draw_end(piet_painting, current_color, num) block = Array.new(12) { Array.new(5) { Array.new(3, 255) } }
old_push_color = current_color.push_color block[0][0] = old_push_color block[0][1] = current_color.write_color block[0...2].each_index do |i| block[i][3] = [0, 0, 0] end block[1][1] = [0, 0, 0] block[2][0] = [0, 0, 0] block[2][4] = [0, 0, 0] block[3][1...4].each_index do |i| block[3][i + 1] = [0, 0, 0] end c_color = current_color.write_color block[2][1...4].each_index do |i| block[2][i + 1] = c_color end pos_y = 12 * num pos_x = 0 piet_painting[pos_x...(pos_x+12)].each_index do |i| piet_painting[pos_x...(pos_x+12)][i][pos_y...(pos_y+5)].each_index do |j| piet_painting[pos_x+i][pos_y+j] = block[i][j] end end end
if painting_len < 390
# plato_painting = Array.new(12 * painting_len) { Array.new(3, 0) }
plato_painting = Array.new(12) { Array.new(painting_len) { Array.new(3, 0) } }
plato_painting[0...12].map! { |row| row[0...painting_len] = piet_painting }
image = MiniMagick::Image.read(plato_painting.flatten.join("\n"))
# image = MiniMagick::Image.new("xc:white")
image.format("png")
- Assistant
To write a Piet compiler, you will need to read the image, analyze the colors, and convert them into the corresponding Piet commands. You can use the ChunkyPNG library to read the images and extract the colors.
Here's a simple Piet compiler that reads the image, analyzes the colors, and calculates the transitions between the colors:
require 'chunky_png' class PietCompiler CTABLE = [ [192, 0, 0], [0, 192, 0], [0, 0, 192], [192, 192, 0], [192, 0, 192], [0, 192, 192], [255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 0, 255], [0, 255, 255] ] def initialize(img_path) @image = ChunkyPNG::Image.from_file(img_path) end def compile y = 0 while y < @image.height x = 0 while x < @image.width color = @image[x, y] x += process_color(x, y, color) end y += 1 end end private def process_color(x, y, color) r, g, b = color_to_rgb(color) idx = find_color_index(r, g, b) if idx return analyze_transition(x, y, idx) else return 1 end end def color_to_rgb(color) [ ChunkyPNG::Color.r(color), ChunkyPNG::Color.g(color), ChunkyPNG::Color.b(color) ] end def find_color_index(r, g, b) CTABLE.index([r, g, b]) end def analyze_transition(x, y, idx) # Add your logic here to analyze the transition and convert it into Piet commands. # This function should return the number of steps the x-coordinate should be moved # to the right to reach the next color block. end end compiler = PietCompiler.new('poem.png') compiler.compile - User
请完善上面代码中的analyze_transition
- Assistant
I have added a simple
analyze_transitionmethod that calculates the difference in hue and lightness levels between the current color and the next color. Please note that this is just a basic implementation to help you get started. You will need to add more logic to handle different color transitions and properly implement the Piet programming language specifications.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.