When creating materials through images, a material color can be obtained.
How to calculate this color value when not creating a material.
Not sure what you want to achieve…There is no color property (method) for Image.
However, the Image#image_rep method returns a copy of a Sketchup::ImageRep object representing the pixel data.
Then you can use
the ImageRep#color_at_uv method to return a color corresponding to the specific UV texture coordinates.
or
the ImageRep#colors method to return an array of Color for each pixel in the image.
…from this color array, with a “simple” average calculation (the average of the r, g, b, a values), you can calculate the average value as if you were retrieving the average of the texture color from the material.
Examples:
A)
path = "Plugins/su_dynamiccomponents/images"
image_file = Sketchup.find_support_file("report_tool.png", path)
image_rep = Sketchup::ImageRep.new(image_file)
colors = image_rep.colors
rs = 0
gs = 0
bs = 0
as = 0
colors.each{|color|
rs += color.red
gs += color.green
bs += color.blue
as += color.alpha
}
avarage_color_rgba = [rs, gs, bs, as].map{|sum| sum/colors.size}
returns: => [115, 124, 134, 187]
B) To compare if you would make material…(but abort it)
m = Sketchup.active_model
m.start_operation("test", true)
path = "Plugins/su_dynamiccomponents/images"
image_file = Sketchup.find_support_file("report_tool.png", path)
material = Sketchup.active_model.materials.add('Example')
material.texture = image_file
avarage_color_rgba = material.color.to_a
m.abort_operation
avarage_color_rgba
returns: => [115, 124, 134, 187]
An image object should already have a material that is image owned and cannot be assigned to another type of entity (doing so with the API will either crash SketchUp or corrupt the file.)
model = Sketchup.active_model
dlist = model.definitions
# Select the image:
i = Sketchup.active_model.selection[0]
# Get the image definition:
idef = dlist.find {|d| d.image? && d.instances.include?(i) }
# Get the image face:
iface = idef.entities.grep(Sketchup::Face).first
# Get the image face material's color:
icolor = iface.material.color
# DO NOT assign this face's material to any other object !
Also here is the code that SketchUp uses for colorizing materials: