1

QUESTION:  Can Emacs help a user to pick colors in the form of a 3-element vector, and what does each element of that vector represent?

Background: I borrowed certain sections of code from pong.el and gamegrid.el, which use a color format of a 3-element vector. At the time this question was initially written, I had never seen colors specified in that format before -- I wanted to use the same system, but had no idea how a particular color could be selected. The following example places four (4) overlays at the beginning of the buffer, but requires a version of Emacs that supports displaying XPM images:

(let* (
    (counter 0)
    xpm-list
    (color-list '(
      [0 0 0]
      [1 1 0]
      [1 0 0]
      [0.5 0.5 0.5]))
    (form-feed-xpm
      "/* XPM */
       static char * form_feed_xpm[] = {
       \"11 20 5 1\",
       \" s None\",
       \"+ s col1\",
       \". s col2\",
       \"% s col3\",
       \"@ s purple\",
       \"%%%%%%%%%%%\",
       \"%.........%\",
       \"%.........%\",
       \"%.........%\",
       \"%.........%\",
       \"%...++++++%\",
       \"%.++.++...%\",
       \"%+...+....%\",
       \"%+...+....%\",
       \"%+...+....%\",
       \"%+...++++.%\",
       \"%....+....%\",
       \"%...++....%\",
       \"%...+.....%\",
       \"%.++++....%\",
       \"%.........%\",
       \"%.........%\",
       \"%.........%\",
       \"%.........%\",
       \"%%%%%%%%%%%\"};")
    (set-color (lambda (color shade)
      (let* ((v (floor (* shade 255)))
       (r (* v (aref color 0)))
       (g (* v (aref color 1)))
       (b (* v (aref color 2))))
        (format "#%02x%02x%02x" r g b))))
    (make-image
      (lambda (color)
        (let* (
            (col1 (funcall set-color color 0.6))
            (col2 (funcall set-color color 0.8))
            (col3 (funcall set-color color 1.0))
            (data `((:type xpm :data ,form-feed-xpm :ascent center
               :color-symbols
                (("col1" . ,col1)("col2" . ,col2) ("col3" . ,col3)))))
            (image (find-image data)) )
          (push image xpm-list)))) )
    (mapcar
      (lambda (x)
        (funcall make-image x))
      color-list)
    (mapcar
      (lambda (x)
        (overlay-put
          (make-overlay
            (+ (point-min) counter) (+ (point-min) (1+ counter)))
          'display x)
        (setq counter (1+ counter)))
      (reverse xpm-list)))
4
  • Are you trying to also create an XPM image, or just need to calculate the color?
    – wvxvw
    Feb 24, 2015 at 5:48
  • @wvxvw -- I am working on a custom minor mode that draws a vertical line to visibly track the cursor position. I have xpm images for every key character on the US/Apple keyboard (and a few that are not). Each xpm image is set to a variable. The colors for each xpm image need to change, depending upon whether the region is active, or whether the vertical line appears on the left or on the right (when cursor is flush with right window edge). I am devising a system to use the same xpm/variable, but color it differently (at least three different ways so far) depending upon the situation.
    – lawlist
    Feb 24, 2015 at 5:52
  • If I understand you correctly, then you need to change image background, right? I'm not 100% sure about how Emacs handles PNGs with transparency, but rather than generating XMPs dynamically, I'd try to use PNGs with transparent areas where the background needs to show through.
    – wvxvw
    Feb 24, 2015 at 10:09
  • @wvxvw -- thank you for the suggestion about using PNGs with transparency -- I will do some reading on that issue. My initial question was poorly phrased, and I have revised both the question and the answer in an effort to be more helpful to other readers in the future. My minor mode is using a system similar to pong.el and gamegrid.el, except that I am placing overlays instead of text-properties and I am not using the buffer-display-table as a warehouse to store XPM images for later usage.
    – lawlist
    Feb 24, 2015 at 18:19

1 Answer 1

1

As stated by @Drew in the comment below this answer, there are a number of libraries that Emacs can use to help a user pick colors -- e.g.:

http://www.emacswiki.org/emacs/download/hexrgb.el

http://www.emacswiki.org/emacs/download/palette.el

colors.el

M-x list-colors-display and then converting the hex shown to a decimal.

As to the issue of what each of the three elements of the color vector represent, the following is a general summary:

The first element of the vector is red.

The second element of the vector is green.

The third element of the vector is blue.

The elements are equal to percentages, with 0 being 0% of that particular color, and 1 being 100% percent of that particular color.

Example: vector color = [1  .5  0] = 100% red, 50% green, 0% blue = orange

2
  • If this is the answer (or an answer), I wonder whether your question is perhaps poorly stated. Is what RGB components correspond to what colors (e.g. named colors) all that you want to know? That information is available by just googling for it. Or by using command list-colors-display (convert the hex shown to decimal). Or by using library color.el or hexrgb.el. Or by any of a number of other Emacs libraries that manipulate colors (e.g. palette.el).
    – Drew
    Feb 24, 2015 at 6:11
  • @Drew -- thank you for pointing me in the right direction. I revised the question and answer in an attempt to make this thread more helpful to others in the future.
    – lawlist
    Feb 24, 2015 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.