4096 HATCH ENTITIES WITH SUBSEQUENT RGB VALUES

4096 HATCH ENTITIES WITH SUBSEQUENT RGB VALUES

Anonymous
Not applicable
1,651 Views
8 Replies
Message 1 of 9

4096 HATCH ENTITIES WITH SUBSEQUENT RGB VALUES

Anonymous
Not applicable

Hello,

 

Recently I wanted to create a script that would help my office to realize the difference between AutoCAD screen colors and actual plot colors. You helped me a lot and I want to share with you the result.

 

RGB_ARRAYRGB_ARRAY

 

The created routine is producing 4096 (16x16x16) hatch rectangles, each having subsequent color in RGB.

(R: 0, G: 0, B: 0), (R: 17, G: 0, B: 0), (R: 34, G: 0, B: 0) ... (R: 255, G: 255, B: 255).

 

Well! The code is working properly, but at first glance seems to me a bit repetitive?

Please test it for yourself and let me know what kind of optimization would you add?

 

;;;;;;;;;;;;;;;;;;;;;;;; FUNCTION TO REMAP RGB COLOR INTO RGB DXF420;;;;;;;;;;;;;;;
(defun RGB->vp-col (l)
  (apply '+
    (mapcar 'lsh
      (if (numberp l) (list 195 0 0 l) (cons 194 l))
      '(24 16 8 0)
    )
  )
)

;;;;;;;;;;;;;;;;;;;;;;;; RGB_ARRAY (R G AND B VALUES);;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun c:RGB_Array_RGB (/	    	
			     	rgb_interval
			    	rgb_hatch
			    	R		G		B
				RGB
			    	global_translation 	i_translation
			    	x_translation 		y_translation			
			    	layername
			    	x1    	x2	x3    	x4
			    	y1    	y2    	y3    	y4	
		    	)
	
  	(setq 	B 			0
		global_translation 	0
	)

  	;;;;;;   CREATES 16 x 2D ARRAYS EACH TIME WITH HIGHER "B" VALUE
	(repeat 16
			(RGB_Array_RG
				rgb_interval
			    	rgb_hatch
			    	R		G		B
				RGB
			    	global_translation 	i_translation
			    	x_translation 		y_translation			
			    	layername
			    	x1    	x2	x3    	x4
			    	y1    	y2    	y3    	y4
			)
	  	(setq B (+ B 17))
	  	(setq global_translation (+ global_translation 2000))
	)
  
)

;;;;;;;;;;;;;;;;;;;;;;;; RGB_ARRAY (ONLY R AND G VALUES) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun RGB_Array_RG	(	    	
			     	rgb_interval
			    	rgb_hatch
			    	R		G		B
				RGB
			    	global_translation 	i_translation
			    	x_translation 		y_translation			
			    	layername
			    	x1    	x2	x3    	x4
			    	y1    	y2    	y3    	y4	
			)
  	
	(setq 	rgb_interval 17
		y_translation 0
		x_translation 0
		i_translation 0
		R 0
		G 0
	)
  
 	;;;;;;;;;;;;;;;;;;;;;;;;;;;;; OUTER REPEAT LOOP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  	;;    CREATES AN 2D ARRAY OF HATCH ENTITIES WITH SUBSEQUENT R and G VALUES    ;;
 	(repeat 16

	  		;;;;;;   HATCH_ENTITY_LAYER_COORDINATES_COLOR
			(setq 	layername "0"
		      		x1 (+ 0 x_translation global_translation)		y1 (+ 0 y_translation)
		      		x2 (+ 100 x_translation global_translation)		y2 (+ 0 y_translation)
		      		x3 (+ 100 x_translation global_translation)		y3 (+ 100 y_translation)
		      		x4 (+ 0 x_translation global_translation)		y4 (+ 100 y_translation)
		     		RGB (list R G B)
		  		RGB (RGB->vp-col RGB)
			)
	  		
			;;;;;;   HATCH_ENTITY
			(setq 	rgb_hatch 
				(list
				(cons 0 "HATCH")		; ENTITY TYPE
				(cons 100 "AcDbEntity")
				(cons 8 layername)		; LAYER NAME
				(cons 100 "AcDbHatch")
				(cons 62 256) 			; COLOR NUMBER
				(cons 10 (list 0.0 0.0 0.0)) 
				(cons 210 (list 0.0 0.0 1.0))
				(cons 2 "SOLID")
				(cons 70 1) 
				(cons 71 0) 
				(cons 91 1) 
				(cons 92 1) 
				(cons 93 4) 
				(cons 72 1) 			; FIRST POINT
				(cons 10 (list x1 y1)) 
				(cons 11 (list x2 y2)) 
				(cons 72 1) 			; SECOND POINT
				(cons 10 (list x2 y2)) 
				(cons 11 (list x3 y3)) 
				(cons 72 1) 			; THIRD POINT
				(cons 10 (list x3 y3)) 
				(cons 11 (list x4 y4)) 
				(cons 72 1) 			; FOURTH POINT
				(cons 10 (list x4 y4)) 
				(cons 11 (list x1 y1)) 
				(cons 97 0) 
				(cons 75 2) 
				(cons 76 1) 
				(cons 98 1) 
				(cons 10 (list 0.0 0.0 0.0)) 
				(cons 470 "LINEAR")
				(cons 420 RGB)
			      	) 
			) ;_end rgb_hatch
			(entmake rgb_hatch) ;_create_hatch
			
	  
		 	;;;;;;;;;;;;;;;;;;;;;;;;;;;;; INNER REPEAT LOOP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	  		;;;;;;   CREATES 15 HATCH ENTITIES, EACH TIME WITH HIGHER G VALUE
		 	(repeat 15

			  		;;;;;;   HATCH_ENTITY_LAYER_COORDINATES_COLOR
					(setq  	x1 (+ 100 i_translation global_translation)		y1 (+ 0 y_translation)
				      		x2 (+ 200 i_translation global_translation)		y2 (+ 0 y_translation)
				      		x3 (+ 200 i_translation global_translation) 		y3 (+ 100 y_translation)
				      		x4 (+ 100 i_translation global_translation) 		y4 (+ 100 y_translation)
						
				      		G (+ G rgb_interval)
				  		RGB (list R G B)
				  		RGB (RGB->vp-col RGB)
						i_translation (+ i_translation 100)	
					)
			  		
					;;;;;;   HATCH_ENTITY
					(setq 	rgb_hatch 
						(list
						(cons 0 "HATCH")		; ENTITY TYPE
						(cons 100 "AcDbEntity")
						(cons 8 layername)		; LAYER NAME
						(cons 100 "AcDbHatch")
						(cons 62 256) 			; COLOR NUMBER
						(cons 10 (list 0.0 0.0 0.0)) 
						(cons 210 (list 0.0 0.0 1.0))
						(cons 2 "SOLID")
						(cons 70 1) 
						(cons 71 0) 
						(cons 91 1) 
						(cons 92 1) 
						(cons 93 4) 
						(cons 72 1) 			; FIRST POINT
						(cons 10 (list x1 y1)) 
						(cons 11 (list x2 y2)) 
						(cons 72 1) 			; SECOND POINT
						(cons 10 (list x2 y2)) 
						(cons 11 (list x3 y3)) 
						(cons 72 1) 			; THIRD POINT
						(cons 10 (list x3 y3)) 
						(cons 11 (list x4 y4)) 
						(cons 72 1) 			; FOURTH POINT
						(cons 10 (list x4 y4)) 
						(cons 11 (list x1 y1)) 
						(cons 97 0) 
						(cons 75 2) 
						(cons 76 1) 
						(cons 98 1) 
						(cons 10 (list 0.0 0.0 0.0)) 
						(cons 470 "LINEAR")
						(cons 420 RGB)
					      	) ;_end list
					) ;_end rgb_hatch
					(entmake rgb_hatch) ;_create_hatch
				) ;_ end INNER LOOP
	  		
	  		(setq	y_translation (+ y_translation 100)
			  	i_translation 0
	  			G 0
				R (+ R rgb_interval)
			)
	) ;_ end OUTER REPEAT LOOP
) ; _end RGB_ARRAY_RG
0 Likes
Accepted solutions (1)
1,652 Views
8 Replies
Replies (8)
Message 2 of 9

doaiena
Collaborator
Collaborator

I can't say it's more optimized than it already was, but at least it's a lot shorter and i find it easier to read/understand like this.

(defun RGB->vp-col (l)
(apply '+
(mapcar 'lsh
(if (numberp l) (list 195 0 0 l) (cons 194 l))
'(24 16 8 0)
)
)
);defun


(defun CreateArray (arrayBase R G B rgbStep size)

;rows
(repeat arraySize
(setq p2 (list (+ (car basePt) size) (cadr basePt))
      p3 (list (+ (car basePt) size) (+ (cadr basePt) size))
      p4 (list (car basePt) (+ (cadr basePt) size))
)

(MakeHatch)

;columns
(repeat (- arraySize 1)
(setq basePt (list (+ (car basePt) size) (cadr basePt))
      p2 (list (+ (car basePt) size) (cadr basePt))
      p3 (list (+ (car basePt) size) (+ (cadr basePt) size))
      p4 (list (car basePt) (+ (cadr basePt) size))
      G (+ G rgbStep)
)

(MakeHatch)
);repeat col

(setq basePt (list (car arrayBase) (+ (cadr basePt) size))
      R (+ R rgbStep)
      G 0
)
);repeat row
);defun


(defun MakeHatch ()

(entmake
(list
(cons 0 "HATCH")		; ENTITY TYPE
(cons 100 "AcDbEntity")
(cons 8 layer)			; LAYER NAME
(cons 100 "AcDbHatch")
(cons 62 256) 			; COLOR NUMBER
(cons 10 (list 0.0 0.0 0.0)) 
(cons 210 (list 0.0 0.0 1.0))
(cons 2 "SOLID")
(cons 70 1) 
(cons 71 0) 
(cons 91 1) 
(cons 92 1) 
(cons 93 4) 
(cons 72 1) 			; FIRST POINT
(cons 10 basePt) 
(cons 11 p2) 
(cons 72 1) 			; SECOND POINT
(cons 10 p2) 
(cons 11 p3) 
(cons 72 1) 			; THIRD POINT
(cons 10 p3) 
(cons 11 p4) 
(cons 72 1) 			; FOURTH POINT
(cons 10 p4) 
(cons 11 basePt) 
(cons 97 0) 
(cons 75 2) 
(cons 76 1) 
(cons 98 1) 
(cons 10 (list 0.0 0.0 0.0)) 
(cons 470 "LINEAR")
(cons 420 (RGB->vp-col (list R G B)))
)
)
);defun


(defun c:RGB_Array_RGB (/ rgbStep arraySize arrayBase R G B RGB basePt layer size dist p2 p3 p4)

(setq layer "0"
      R 0
      G 0
      B 0
      rgbStep 17
      arraySize 16
      size 100
      dist 400
      arrayBase (list 0.0 0.0)
      basePt arrayBase
)

(repeat 16
(CreateArray arrayBase R G B rgbStep size)
(setq B (+ B rgbStep)
      arrayBase (list (+ (car arrayBase) dist (* arraySize size)) 0.0)
      basePt arrayBase
)
)

(princ)
);defun

 

Message 3 of 9

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

.... The code is working properly, but at first glance seems to me a bit repetitive?

Please test it for yourself and let me know what kind of optimization would you add?

....


Pardon my suggesting an entirely different approach [rather than optimization(s)], but this is so much shorter....  It uses the (assoc 420) entity data values, which are single integers  [the tricky part being to figure out the incrementing to make the right kinds of shifts at the right places], rather than spelling out RGB lists-of-three  in the "usual" way.  It makes SOLIDs rather than HATCHes [less memory], and its progression moves in a different way, but I believe it hits them all, starting as your other one does with 0,0,0 at the bottom left and ending with 255,255,255 at the top right.

(defun C:4096Colors (/ baseG baseR)
  (setq baseG '(-100 0 0) baseR baseG rgb -17)
  (repeat 16 ; larger-square Groups
    (repeat 16 ; Rows in a group
      (setq LL baseR)
      (repeat 16 ; Solids in a row
        (entmake
          (list
            '(0 . "SOLID")
            (cons 10 (setq LL (polar LL 0 100))) ; new LL
            (cons 11 (polar LL 0 100)) ; LR
            (cons 12 (polar LL (/ pi 2) 100)) ; UL
            (cons 13 (mapcar '+ LL '(100 100))) ; UR
            (cons 420 (setq rgb (+ rgb 17)))
          ); list
        ); entmake
      ); repeat [Solids]
      (setq baseR (polar baseR (/ pi 2) 100) rgb (+ rgb 4080)); move up, jump-shift color
    ); repeat [Rows]
    (setq baseG (polar baseG 0 1700) baseR baseG rgb (+ rgb 1044480))
; move right for next Group, jump-shift color ); repeat [Groups] (princ) ); defun

Result looks like this:

rgb.PNG

Kent Cooper, AIA
Message 4 of 9

Kent1Cooper
Consultant
Consultant

Here's the "system" of color-number progression:

RGBsystem.PNG

Kent Cooper, AIA
Message 5 of 9

Kent1Cooper
Consultant
Consultant

Another way to do it....  The (assoc 420) value is the sum of the Red value in the RGB list x 256 squared, plus the Green value x 256, plus the Blue value.  This uses that relationship, without the jump-shift additions:

(defun C:4096Colors (/ baseG baseR ML LL)
  (setq baseG '(-100 0 0) baseR baseG ML '(0 0 0))
    ; ML = Multiplier List for RGB components of (assoc 420) values
  (repeat 16 ; larger-square Groups
    (repeat 16 ; Rows in a group
      (setq LL baseR)
      (repeat 16 ; Solids in a row
        (entmake
          (list
            '(0 . "SOLID")
            (cons 10 (setq LL (polar LL 0 100))) ; new LL
            (cons 11 (polar LL 0 100)) ; LR
            (cons 12 (polar LL (/ pi 2) 100)) ; UL
            (cons 13 (mapcar '+ LL '(100 100))) ; UR
            (cons 420 (+ (* (expt 256 2) 17 (car ML)) (* 256 17 (cadr ML)) (* 17 (caddr ML))))
          ); list
        ); entmake
        (setq ML (mapcar '+ ML '(0 0 1))); increment Blue multiplier
      ); repeat [Solids]
      (setq
        baseR (polar baseR (/ pi 2) 100); move up a row
        ML (list (car ML) (1+ (cadr ML)) 0); increment Green; Blue back to 0 for new row
      ); setq
    ); repeat [Rows]
    (setq
      baseG (polar baseG 0 1700); move right for next group
      baseR baseG ; bottom row again
      ML (list (1+ (car ML)) 0 0); increment Red; Green & Blue back to 0 for new group
    ); setq
  ); repeat [Groups]
  (princ)
); defun
Kent Cooper, AIA
0 Likes
Message 6 of 9

Anonymous
Not applicable

I was not sure how to avoid the repetition, but it seems much clearer after your improvements.

Pure gold! Thank you once again.

0 Likes
Message 7 of 9

Anonymous
Not applicable

I studied your code and I don't understand how this TrueColor value is created:

(cons 420 (+ (* (expt 256 2) 17 (car ML)) (* 256 17 (cadr ML)) (* 17 (caddr ML))))

What is the theory behind it?

0 Likes
Message 8 of 9

doaiena
Collaborator
Collaborator

That is a formula to convert the (R G B) color format into an integer number. (255, 255, 255) is represented as 16777215, which is the largest 24bit integer. Out of the 24 bits, representing true color, the first 8 bits /0-7/ represent the blue component, the next 8 bits /8-15/ are the green component, the last 8 bits /16-23/ are the red component /counting back to front/.

 

(255, 255, 255) 16777215 represented in binary is 1111 1111 1111 1111 1111 1111

 

(255, 0, 0) 16711680 in binary is 1111 1111 0000 0000 0000 0000 /the 8 bits of the red component/

(0, 255, 0)      65280 in binary is 0000 0000 1111 1111 0000 0000 /the 8 bits of the green component/
(0, 0, 255)           255 in binary is 0000 0000 0000 0000 1111 1111 /the 8 bits of the blue component/

 

The formula to convert (R G B) to an integer is:
(+ (* R 65536) (* G 256) B)

 

@Kent1Cooper 's formula looks a bit more diffucult to understand, because he has added your rgb step of 17. He has also written 65536 as (expt 256 2).

Message 9 of 9

Anonymous
Not applicable

Wow! Thank you! Smooth explanation.

0 Likes