True color property of an entity is stored under the DXF code of 420. It is a 32-bit integer value. When used with True Color, the 32-bit integer represents a 24-bit color value. The high-order byte (8 bits) is 0, the low-order byte an unsigned char holds the Blue value (0-255), then comes the Green value, and the next-to-high order byte is the Red Value. Converting this integer value to hexadecimal yields the following bit mask: 0x00RRGGBB. For example, a true color with Red = 200, Green = 100 and Blue = 50 is 0x00C86432, and in DXF, in decimal, 13132850.
The following code demonstrates how to convert a list of rgb to a true color and vice versa, and shows how to retreive/set a true color property to an entity, using AutoLISP.
;; Convert TrueColor into a list of RGB
(defun trueColorToRGB (tcol)
(setq r (lsh tcol -16))
(setq g (lsh (lsh tcol 16) -24))
(setq b (lsh (lsh tcol 24) -24))
(list r g b)
)
;; Convert a list of RGB to TrueColor
(defun RGBToTrueColor (rgb)
(setq r (lsh (car rgb) 16))
(setq g (lsh (cadr rgb) 8))
(setq b (caddr rgb))
(setq tcol (+ (+ r g) b))
)
(defun C:getColor(/)
(setq ename (car (entsel "\nPick an object with true color:")))
(setq edata (entget ename))
;; we have a true color.
(setq tcol (cdr (assoc 420 edata)))
(princ "\n true color = ")(princ tcol)
;; convert it to a
list of RGB.
(setq rgb (trueColorToRGB tcol))
(princ "\n rgb = ")(princ rgb)
(princ)
)
(defun C:setColor(/)
(setq ename (car (entsel "\nPick an object to set a true color:")))
(setq edata (entget ename))
;; set a true color from a list of rgb values.(R=10 G=100 B=200)
(setq rgb (list 10 100 200))
(setq tcol (RGBToTrueColor rgb))
;; and set it.
(setq edata (subst (cons 420 tcol) (assoc 420 edata) edata))
(entmod edata)
(princ "\n rgb = ")(princ rgb)
(princ "\n true color = ")(princ tcol)
(princ)
)

Leave a Reply to Gopinath TagetCancel reply