module IT_Mat_samples
    def self.generate_samples (tipe)
    @tip = tipe
        @model=Sketchup.active_model
        @entities=@model.entities
        layers = @model.layers
        user_layer = @model.active_layer
 layer_samples = layers['_Materials Samples']

if layer_samples.class == NilClass
    layer_samples = @model.layers.add('_Materials Samples')
    layer_samples.visible = true
end
@model.active_layer = layer_samples
        @sample_height="800mm"
        @sample_width="800mm"
        @samp_offset="1200mm"
        @sample_height=Sketchup.parse_length(@sample_height)
        @sample_width=Sketchup.parse_length(@sample_width)
        @samp_offset=Sketchup.parse_length(@samp_offset)
        mat_solid = []
        mat_textur = []
        mat_all=@model.materials
        mat_all.each{|mat|
        if (mat.texture.respond_to?(:filename)) 
          mat_textur << mat 
          else
          mat_solid << mat
          end
          }
if mat_textur.size 
mat_textur_sort = mat_textur.sort_by{|mat| [mat.texture.filename.size , mat.texture.filename] }
end

if mat_solid.size 

mat_solid_sort = mat_solid.sort_by{|mat| rgb_hsl(mat.color.red, mat.color.green, mat.color.blue)}.reverse
end


@materials=mat_solid_sort+mat_textur_sort if @tip =~ /color/ 
@materials=@model.materials.sort_by{|mat| mat.display_name }.reverse if @tip == 'name'
          
        @pt1=Geom::Point3d.new(0,0,0)
        @pt2=Geom::Point3d.new(@sample_width,0,0)
        @pt3=Geom::Point3d.new(@sample_width,@sample_height,0)
        @pt4=Geom::Point3d.new(0,@sample_height,0)
        
        @txt_pos_offset="200mm"
        @txt_pos_offset=Sketchup.parse_length(@txt_pos_offset)
        @txt_offset_vec=Geom::Vector3d.new(@txt_pos_offset,0,0)
        
        curr_offset=0.0
        @model.start_operation("Materials Samples Generation")
        @materials.each{|mat|
            sample_group=@entities.add_group
                 if (mat.texture.respond_to?(:filename))
        @pt1=Geom::Point3d.new(0,0,0)
        @pt2=Geom::Point3d.new(mat.texture.width,0,0)
        @pt3=Geom::Point3d.new(mat.texture.width,mat.texture.height,0)
        @pt4=Geom::Point3d.new(0,mat.texture.height,0)
        else
        @pt1=Geom::Point3d.new(0,0,0)
        @pt2=Geom::Point3d.new(@sample_width,0,0)
        @pt3=Geom::Point3d.new(@sample_width,@sample_height,0)
        @pt4=Geom::Point3d.new(0,@sample_height,0)
        
        end #if
            sample_face=sample_group.entities.add_face([@pt1, @pt2, @pt3, @pt4])
            sample_face.reverse!
            sample_face.material=mat
            sample_face.back_material=mat
            if (mat.texture.respond_to?(:filename))
            asp=mat.texture.width.to_f/mat.texture.height
        t = Geom::Transformation.scaling(sample_group.bounds.corner(0).clone,((@sample_width*asp)/sample_group.bounds.width).to_f,@sample_height/sample_group.bounds.height.to_f,1)
        sample_group.transform!(t)
            end #if
            curr_offset+=@samp_offset
            offset_vec=Geom::Vector3d.new(0,curr_offset,0)
            offset_tr=Geom::Transformation.new(offset_vec)
            sample_group.transform!(offset_tr)
            sample_group.name=mat.name
            
            txt_pos=sample_group.bounds.max.offset(@txt_offset_vec)  
            
            @entities.add_text(mat.name.to_s, txt_pos)
        }
        @model.active_layer = user_layer
        @model.commit_operation
    end
    def self.rgb_hsl(r,g,b)
var_R = r/255.0                                    #RGB from 0 to 255
var_G = g/255.0
var_B =  b/255.0
var_Min =  [var_R, var_G, var_B].min     #Min. value of RGB
var_Max = [var_R, var_G, var_B].max    #Max. value of RGB
del_Max = var_Max - var_Min                #Delta RGB value
l = ( var_Max + var_Min ) / 2.0
if ( del_Max == 0 )                             #This is a gray, no chroma...
h = 0                                                 #hsl results from 0 to 1
s = 0
else                                                   #Chromatic data...
   if ( l < 0.5 ) 
       s = del_Max/(var_Max + var_Min)
       else 
       s = del_Max/(2.0 - var_Max - var_Min)
 end #if
   del_R = (((var_Max-var_R)/6.0) + (del_Max/2.0))/del_Max
   del_G = (((var_Max-var_G)/6.0) + (del_Max/2.0))/del_Max
   del_B = (((var_Max-var_B)/6.0) + (del_Max/2.0))/del_Max
h = del_B - del_G if  (var_R == var_Max)
h = (1 / 3.0) + del_R - del_B if (var_G == var_Max)
h = (2 / 3.0) + del_G - del_R if (var_B == var_Max) 
    h += 1 if (h < 0)
    h -= 1 if (h > 1) 
    h = 1 if (h == 0) and (var_R == var_Max)
 end #if
h = (h*360).to_i
s = (s*10).to_i
l = (l*20).to_i


l= 20-l + 10000 if s == 0 && @tip == 'color1'
h = 1000 - l if s == 0 && @tip == 'color'
# return   h,s,l
return  l,h,s if @tip == 'color1'
return  h,l,s
end #def
end #module IT_Mat_samples


if( $submenu )
    organizer = $submenu 
    if !file_loaded?(__FILE__) then
        organizer.add_item ('Generate Materials Samples by Color Hue') { IT_Mat_samples.generate_samples 'color'}
       organizer.add_item ('Generate Materials Samples by Color Lightness') { IT_Mat_samples.generate_samples 'color1'}
        
organizer.add_item ('Generate Materials Samples by Name') { IT_Mat_samples.generate_samples 'name' }
    end
else
    if !file_loaded?('ithil_menu_loader') then
        $ithil_menu = UI.menu("Plugins").add_submenu("Ithil Tools")
    end
    if !file_loaded?(__FILE__) then
        $ithil_menu.add_item ('Generate Materials Samples by Color Hue') { IT_Mat_samples.generate_samples 'color'}
       $ithil_menu.add_item ('Generate Materials Samples by Color Lghtness') { IT_Mat_samples.generate_samples 'color1'}
        $ithil_menu.add_item ('Generate Materials Samples by Name') { IT_Mat_samples.generate_samples 'name' }
    end
end
file_loaded('ithil_menu_loader')
file_loaded(__FILE__)

