Automate Color Variations in SketchUp with Ease
-
Automate color variations in SketchUp with ease
This simple SketchUp script is designed to help you easily generate and visualize color variations for a selected object. The script creates three sets of variations: luminance, hue, and saturation. Each set forms a circle of rectangles around the original object, illustrating different levels of brightness, color, and saturation changes. Just select a face with any desired color and run the script to see the variations. It's a simple tool that can help you explore color options more effectively in your models.
require 'sketchup' module CombinedVariations extend self def create_variations create_luminance_variations create_color_variations create_saturation_variations end def create_luminance_variations puts "Iniciando a criação de variações de luminância..." model = Sketchup.active_model selection = model.selection unless selection.length == 1 && selection[0].is_a?(Sketchup::Face) puts "Por favor, selecione apenas um objeto com uma cor só." return end puts "Objeto selecionado encontrado." original_color = selection[0].material.color center_point = selection[0].bounds.center circle_radius = 400.cm puts "Cor original do objeto: #{original_color}" puts "Criando retângulos com variações de luminância..." colors = generate_luminance_colors(original_color) create_rectangles_around_object(center_point, circle_radius, colors, 'LUM') puts "Variações de luminância criadas com sucesso." end def generate_luminance_colors(original_color) puts "Gerando variações de luminância..." colors = [] hsl_color = rgb_to_hsl(original_color.red, original_color.green, original_color.blue) original_hue, original_saturation, original_lightness = hsl_color[:h], hsl_color[:s], hsl_color[:l] luminance_stages = (0..100).map { |i| i / 100.0 } selected_luminance_stages = luminance_stages.select.with_index { |_, i| i.even? } selected_luminance_stages.each do |lightness| color = hsl_to_rgb(original_hue, original_saturation, lightness) colors << Sketchup::Color.new(color[:r], color[:g], color[:b]) end colors end def create_color_variations puts "Iniciando a criação de variações de cores..." model = Sketchup.active_model selection = model.selection unless selection.length == 1 && selection[0].is_a?(Sketchup::Face) puts "Por favor, selecione apenas um objeto com uma cor só." return end puts "Objeto selecionado encontrado." original_color = selection[0].material.color center_point = selection[0].bounds.center circle_radius = 500.cm puts "Cor original do objeto: #{original_color}" puts "Criando retângulos com variações de cores..." colors = generate_color_variations(original_color) create_rectangles_around_object(center_point, circle_radius, colors, 'HUE') puts "Variações de cores criadas com sucesso." end def generate_color_variations(original_color) puts "Gerando variações de cores..." hue_step = 360.0 / 64.0 colors = [] hsv_color = rgb_to_hsv(original_color.red, original_color.green, original_color.blue) original_hue, original_saturation, original_value = hsv_color[:h], hsv_color[:s], hsv_color[:v] 64.times do |i| hue = (original_hue + i * hue_step) % 360 color = hsv_to_rgb(hue, original_saturation, original_value) colors << Sketchup::Color.new(color[:r], color[:g], color[:b]) end colors end def create_saturation_variations puts "Iniciando a criação de variações de saturação..." model = Sketchup.active_model selection = model.selection unless selection.length == 1 && selection[0].is_a?(Sketchup::Face) puts "Por favor, selecione apenas um objeto com uma cor só." return end puts "Objeto selecionado encontrado." original_color = selection[0].material.color center_point = selection[0].bounds.center circle_radius = 300.cm puts "Cor original do objeto: #{original_color}" puts "Criando retângulos com variações de saturação..." colors = generate_saturation_colors(original_color) create_rectangles_around_object(center_point, circle_radius, colors, 'SAT') puts "Variações de saturação criadas com sucesso." end def generate_saturation_colors(original_color) puts "Gerando variações de saturação..." colors = [] hsl_color = rgb_to_hsl(original_color.red, original_color.green, original_color.blue) original_hue, original_saturation, original_lightness = hsl_color[:h], hsl_color[:s], hsl_color[:l] saturation_stages = (0..35).map { |i| i / 35.0 } saturation_stages.each do |saturation| color = hsl_to_rgb(original_hue, saturation, original_lightness) colors << Sketchup::Color.new(color[:r], color[:g], color[:b]) end colors end def rgb_to_hsl(r, g, b) r, g, b = r / 255.0, g / 255.0, b / 255.0 max = [r, g, b].max min = [r, g, b].min delta = max - min l = (max + min) / 2.0 if delta == 0 h = 0 s = 0 else s = delta / (1 - (2 * l - 1).abs) h = if max == r ((g - b) / delta) % 6 elsif max == g (b - r) / delta + 2 else (r - g) / delta + 4 end h *= 60 h += 360 if h < 0 end { h: h, s: s, l: l } end def hsl_to_rgb(h, s, l) c = (1 - (2 * l - 1).abs) * s x = c * (1 - ((h / 60.0) % 2 - 1).abs) m = l - c / 2.0 r, g, b = case h when 0..60 then [c, x, 0] when 60..120 then [x, c, 0] when 120..180 then [0, c, x] when 180..240 then [0, x, c] when 240..300 then [x, 0, c] else [c, 0, x] end { r: ((r + m) * 255).round, g: ((g + m) * 255).round, b: ((b + m) * 255).round } end def rgb_to_hsv(r, g, b) r, g, b = r / 255.0, g / 255.0, b / 255.0 max = [r, g, b].max min = [r, g, b].min delta = max - min h = if max == min 0 elsif max == r 60 * (((g - b) / delta) % 6) elsif max == g 60 * (((b - r) / delta) + 2) else 60 * (((r - g) / delta) + 4) end s = max == 0 ? 0 : delta / max v = max { h: h, s: s, v: v } end def hsv_to_rgb(h, s, v) h = h / 60.0 i = h.floor f = h - i p = v * (1 - s) q = v * (1 - f * s) t = v * (1 - (1 - f) * s) r, g, b = case i % 6 when 0 then [v, t, p] when 1 then [q, v, p] when 2 then [p, v, t] when 3 then [p, q, v] when 4 then [t, p, v] when 5 then [v, p, q] end { r: (r * 255).to_i, g: (g * 255).to_i, b: (b * 255).to_i } end def create_rectangles_around_object(center_point, circle_radius, colors, prefix) puts "Criando retângulos em torno do objeto..." model = Sketchup.active_model entities = model.active_entities rectangle_size = [35.cm, 35.cm] angle_step = 2 * Math::PI / colors.length current_angle = 0 k = 0 colors.each do |color| x = center_point.x + circle_radius * Math.cos(current_angle) y = center_point.y + circle_radius * Math.sin(current_angle) position = Geom::Point3d.new(x, y, center_point.z) group = entities.add_group pt1 = position pt2 = Geom::Point3d.new(x + rectangle_size[0], y, center_point.z) pt3 = Geom::Point3d.new(x + rectangle_size[0], y + rectangle_size[1], center_point.z) pt4 = Geom::Point3d.new(x, y + rectangle_size[1], center_point.z) face = group.entities.add_face(pt1, pt2, pt3, pt4) face.reverse! material_name = "#{prefix}_#{'%02d' % k}_#{color.red}_#{color.green}_#{color.blue}" material = model.materials[material_name] || model.materials.add(material_name) material.color = color face.material = material current_angle += angle_step k += 1 end puts "Retângulos criados." end end UI.menu("Edit").add_item("Create Combined Variations") { CombinedVariations.create_variations }
Advertisement