From: Xah Lee
Subject: Re: elisp function for hexidecimal to decimal?
Date: 
Message-ID: <1176193001.148696.82120@e65g2000hsc.googlegroups.com>
Thanks Miles Bader.

With your help, i've wrote the following function.

(defun rgb ()
 "Convert a RGB color value in this notation #ff6916 to a decimal 0 to
1 system, replacing the word the cursor is on."
 (interactive)
 (let (rgb boundy)
   (setq rgb (thing-at-point 'word))
   (setq boundy (bounds-of-thing-at-point 'word))
   (delete-region (car boundy) (cdr boundy))
   (insert
    (format "%s"
            (mapcar (lambda (x) (/ x 65535.0)) (color-values (concat
"#" rgb)))
            )
    )
   )
 )

... in the (format "%s" mylistOfDecimals), is there a way to truncate
the decimale to just 2 places?

Miles wrote:
«People who are more than casually interested in computers should have
at least some idea of what the underlying hardware is like. Otherwise
the programs they write will be pretty weird.» — Donald Knuth

Fuck Donald Knuth.

  Xah
  ···@xahlee.org
∑ http://xahlee.org/


On Apr 9, 3:35 pm, Miles Bader <····@gnu.org> wrote:
> "XahLee" <····@xahlee.org> writes:
> > is there function that converts hexidecimal to decimal?
> > i need the function to write a elisp so that
> > "#ff6916"
> > becomes
> > "255,105,180"
> > This is for converting RGB color on the 0-255 system to 0-1 system.
>
> There's actually a built-in function that does exactly that,
> "color-values":
>
>    (color-values "#ff6916")
>      => (65280 26880 5632)
>
> Note that the value range is nominally 0-65535 (from the example, I
> notice it's doing the conversion slightly incorrectly though... :-O )
>
> To convert to floating point values in the range 0-1 you could do:
>
>    (mapcar (lambda (v) (/ v 65535.0)) (color-values "#ff6916"))
>      => (0.9961089494163424 0.4101625085831998 0.08593881132219425)
>
> For general hex-conversion, you can use "string-to-number", which takes
> the number base as a second argument:
>
>    (string-to-number "ff" 16)
>      => 255
>    (string-to-number "69" 16)
>      => 105
>    (string-to-number "16" 16)
>      => 22
>
> -Miles
> --