• Login
sketchucation logo sketchucation
  • Login
๐Ÿค‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Wrong cloning of Sketchup::Color objects

Scheduled Pinned Locked Moved Developers' Forum
9 Posts 4 Posters 1.2k Views 4 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    fredo6
    last edited by 15 Apr 2016, 15:11

    For information, there is an error in the way colors created as Sketchup::Color objects answer to the clone method.

    Easy to reproduce in the Ruby console (all versions of SU, including SU2016)

    
    c = Sketchup;;Color.new 'blue'
    Color(  0,   0, 255, 255)
    c.clone
    Color(  0,   0,   0,   0)
    
    

    This is annoying if you do not know the problem and duplicate hash arrays by cloning.

    The workaround is simply cloned_color = Sketchup::Color.new(original_color).

    But, I see no metaphysical reason why clone would not work correctly. It works fine on Point3d, Vector3d and Transformations. So I logged a bug to the Sketchup team.

    Fredo

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 15 Apr 2016, 21:39

      hm... I think I ran into this myself. Might be an omission.

      Btw, in regard to point3d and vector3d - one would normally use dup - but for some reason only clone was implemented for these classes and I think it acts like dup should have been.

      Thomas Thomassen โ€” SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • D Offline
        driven
        last edited by 15 Apr 2016, 23:08

        I use

         c = (Sketchup;;Color.new 'blue').to_a
        # => [0, 0, 255, 255]
         c.dup
        # => [0, 0, 255, 255]
        

        can't remember why, but maybe for the same reason...

        john

        learn from the mistakes of others, you may not live long enough to make them all yourself...

        1 Reply Last reply Reply Quote 0
        • T Offline
          thomthom
          last edited by 16 Apr 2016, 11:46

          c2 = Sketchup::Color.new(c1.to_a)

          Thomas Thomassen โ€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • F Offline
            fredo6
            last edited by 16 Apr 2016, 18:02

            The workaround is easy to find, but that was the bug which was more difficult to identify.

            I still think it is a bug. Either Sketchup::Color does not support clone, or it does support correctly!

            Fredo

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 16 Apr 2016, 23:00

              True that.

              Thomas Thomassen โ€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • F Offline
                fredo6
                last edited by 23 Apr 2016, 19:56

                Dan,

                We know that most Sketchup objects won't behave correctly with clone or dup.

                However, and wrongly, I thought Sketchup::Color was just an encapsulation of a 4-value array and would behave as Geom::Point3d or Geom::Transformation.

                Now I know it's not the case and I can live with it, because I know.

                Fredo

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Dan Rathbun
                  last edited by 24 Apr 2016, 22:14

                  @thomthom said:

                  hm... I think I ran into this myself. Might be an omission.

                  Yes, you did. You posted a public announcement in 2010:
                  http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=32591

                  @fredo6 said:

                  This is annoying if you do not know the problem and duplicate hash arrays by cloning.

                  The workaround is simply cloned_color = Sketchup::Color.new(original_color).

                  Which goes against the API documentation be inadequate (again.)

                  @fredo6 said:

                  It works fine on Point3d, Vector3d and Transformations.

                  Because the API explicitly overrides the clone() method inherited from object, in those three API classes.
                  http://ruby-doc.org/core-2.0.0/Object.html#method-i-clone

                  @fredo6 said:

                  But, I see no metaphysical reason why clone would not work correctly.

                  The Ruby interpreter follows precise "rules" of it's Object Oriented Programming model, .. so metaphysics (or abstract) reasons do not apply. ๐Ÿ˜‰

                  In the case of Ruby's clone and dup

                  @unknownuser said:

                  Ruby Doc: Object#clone()[/url:3ety1hw5]":3ety1hw5]This method may have class-specific behavior. If so, that behavior will be documented under the #initialize_copy method of the class.

                  So, a custom class needs to implement an #initialize_copy() method, in order to leverage Ruby's built-in cloning / dup'ing (so Ruby knows how to create the object copy,) or locally override the methods directly.

                  For examples of how arrays, hashes and strings are cloned see:

                  • [url=http://ruby-doc.org/core-2.0.0/Hash.html#method-i-initialize_copy:3ety1hw5 ]#initialize_copy (Hash)[/url:3ety1hw5] [you must click to see the C source]

                  Just making an assumption, that is outside the "order of Ruby things" is bound to lead to failure or frustration.

                  So the historical correct assumption has been, "If API classes (especially those that are model entity classes,) have not published a specific cloning method overrides,.. then don't use clone() or dup() with them."

                  How do you know when API classes implement clone()?

                  Refer to the API method index for "c":
                  http://www.sketchup.com/intl/en/developer/docs/methods#index_c

                  @fredo6 said:

                  So I logged a bug to the Sketchup team.

                  Which I also did like 7/8 years ago. So, it is already logged.

                  Other old threads on this:
                  2010: http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=29138%26amp;p=253940#p253787
                  2011: http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=40201%26amp;p=355527#p355527
                  2014: http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=58083%26amp;hilit=clone%26amp;p=530348#p530015


                  Related... concerning lack of Geom::PolygonMesh clone() & dup() methods:
                  http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=58080%26amp;p=530014#p530014

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • D Offline
                    Dan Rathbun
                    last edited by 24 Apr 2016, 22:28

                    @fredo6 said:

                    However, and wrongly, I thought Sketchup::Color was just an encapsulation of a 4-value array ...

                    But that does bring up a very good point, that because it is compatible with an array (ie, has a to_a method and it's constructor accepts an array,)... making a clone method is beyond simple:

                    For Ruby 2.0+;

                    module Fredo6;;Cloner
                      refine ;;Sketchup;;Color do
                        def clone()
                          self.class;;new(self.to_a)
                        end
                      end
                    end
                    
                    using Fredo6;;Cloner
                    
                    module Fredo6;;SomePlugin
                      # code that uses refined Color class
                    end
                    
                    

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    1 / 1
                    • First post
                      1/9
                      Last post
                    Buy SketchPlus
                    Buy SUbD
                    Buy WrapR
                    Buy eBook
                    Buy Modelur
                    Buy Vertex Tools
                    Buy SketchCuisine
                    Buy FormFonts

                    Advertisement