Jump to content

RGB to HEX


Anushka

Recommended Posts

That's actually an interesting one, and I'm quite surprised that there's no solution in Google anywhere for AutoLISP.

 

Let me be the first then:

 

;; JH:RGB->HEX --> Jonathan Handojo
;; Converts an RGB to HEX in the form of a string

(defun JH:RGB->HEX (r g b)
  (apply 'strcat (mapcar '(lambda (x / h) (if (< (strlen (setq h (JH:numhex x))) 2) (strcat "0" h) h))  (list r g b)))
  )

;; JH:numhex --> Jonathan Handojo
;; Converts an integer to a hex number in the form of a string

(defun JH:numhex (num / hex)
  (setq hex (mapcar 'cons '(10 11 12 13 14 15) (mapcar 'chr '(65 66 67 68 69 70))))
  (apply 'strcat
	 ((lambda (x / l h)
	    (if (zerop x) '("0")
	      (progn
		(while (/= x 0)
		  (setq l (cons (cond ((cdr (assoc (setq h (rem x 16)) hex))) ((itoa h))) l)
			x (/ x 16)
			)
		  )
		l
		)
	      )
	    )
	   num
	   )
	 )
  )

 

I'm sure someone has a better approach.

 

_$ (JH:RGB->HEX 45 89 142)
"2D598E"

 

  • Thanks 2
Link to comment
Share on other sites

For example:

(defun rgb->hex ( r g b )
    (apply 'strcat
        (mapcar '(lambda ( x ) (setq x (strcat "00" x)) (substr x (1- (strlen x))))
            (mapcar 'LM:dec->hex (list r g b))
        )
    )
)

;; Decimal to Hexadecimal  -  Lee Mac
;; Converts a decimal number to hexadecimal string
;; d - [int] 32-bit signed integer
;; Returns: [str] Hexadecimal representation of supplied integer

(defun LM:dec->hex ( d )
    (if (< d 16)
        (chr (+ d (if (< d 10) 48 55)))
        (strcat (LM:dec->hex (/ d 16)) (LM:dec->hex (rem d 16)))
    )
)
_$ (rgb->hex 255 0 0)
"FF0000"

 

  • Thanks 1
Link to comment
Share on other sites

1 hour ago, Anushka said:

My XML asks for an extra value "I think it's opacity"
so my red is like this FF0000FF, how can I deal with it ???

 

Simply concatenate the opacity value?

Link to comment
Share on other sites

49 minutes ago, Lee Mac said:

 

Simply concatenate the opacity value?

 

Opacity of 255? I thought in AutoCAD it's only up to 100 (as in percentage)

Link to comment
Share on other sites

27 minutes ago, Jonathan Handojo said:

Opacity of 255? I thought in AutoCAD it's only up to 100 (as in percentage)

 

I imagine that the OP is not using the output to set transparency in AutoCAD, but for some other application which uses RGBA colour coding.

  • Like 1
Link to comment
Share on other sites

3 hours ago, Lee Mac said:

 

I imagine that the OP is not using the output to set transparency in AutoCAD, but for some other application which uses RGBA colour coding.

 

 

Thanks from wiki ,  ARGB max A is FF, but my test in CAD max just reach 7F??

 

PixelSamples32bppRGBA.png.df3481c8538c45f923d183d4d762cb0c.png

 

;;https://en.wikipedia.org/wiki/RGBA_color_model
(defun wiki:rgb (lst)
  
  ((lambda (i)
     (apply 'logior
	    (mapcar '(lambda (x) (lsh (fix x) (setq i (- i 8)))) lst)
     )
   )
    (* (length lst ) 8)
  )
)

;eg
;(wiki:rgb (list 45 89 142))
;2972046 

example : R,G,B,A =  45,89,142,127

(mapcar 'set '(r g b a) '(45 89 142 127))
(LM:dec->hex (wiki:rgb (list a r g b)) )

"7F2D598E" --> AGRB

but if A>127, i.e: (128 ~ 255 )

"¾"  ???

 

maybe concatenate either "A+RGB "  or  "RGB+A"

in ARGB case if A=128 

(apply 'strcat
     (mapcar ''((x) (LM:dec->hex x))
         (list 128 (wiki:rgb (list r g b)))
     )
  )

 

 

 

 

 

 

 

Edited by hanhphuc
syntax color
  • Thanks 1
Link to comment
Share on other sites

2 hours ago, Lee Mac said:

Hint: integers in AutoLISP are represented using signed 32-bit integers, unsigned integers aren't available.

 

Thanks Lee! +2,147,483,647 to -2,147,483,648 

 

p/s:  rgb reminds me UTF-8 time flies! 

4.18? cheers guru 🍻 :)

 

Edited by hanhphuc
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...