1 ;;; jabber-muc-nick-coloring.el --- Add nick coloring abilyty to emacs-jabber -*- lexical-binding: t; -*-
3 ;; Copyright 2009, 2010, 2012, 2013 Terechkov Evgenii - evg@altlinux.org
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; if not, write to the Free Software
17 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 ;;;;##########################################################################
26 ;;;; User Options, Variables
27 ;;;;##########################################################################
29 (defcustom jabber-muc-participant-colors nil
30 "Alist of used colors.
31 Format is (nick . color). Color may be
32 in #RGB or textual (like red or blue) notation. Colors will be
33 added in #RGB notation for unknown nicks."
34 :type '(alist :key-type string :value-type color)
37 (defcustom jabber-muc-colorize-local nil
38 "Colorize MUC messages from you."
42 (defcustom jabber-muc-colorize-foreign nil
43 "Colorize MUC messages not from you."
47 (defcustom jabber-muc-nick-saturation 1.0
48 "Default saturation for nick coloring."
52 (defcustom jabber-muc-nick-value 1.0
53 "Default value for nick coloring."
57 (defun jabber-muc-nick-hsv-to-hsl (h s v)
58 "Convert color consisting of H, S and V to list of HSL values."
59 ;; https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL
61 (luminance (* v (- 1 (/ s 2.0))))
62 (saturation (if (or (= luminance 0) (= luminance 1))
64 (/ (- v luminance) (min luminance (- 1 luminance))))))
65 (list hue saturation luminance)))
67 (defun jabber-muc-nick-gen-color (nick)
68 "Return a good enough color from the available pool."
69 (let* ((pool-index (mod (string-to-number (substring (md5 nick) 0 6) 16) 360))
70 (hue (/ pool-index 360.0))
71 (saturation jabber-muc-nick-saturation)
72 (value jabber-muc-nick-value)
73 (hsl (jabber-muc-nick-hsv-to-hsl hue saturation value)))
74 (apply #'color-rgb-to-hex (apply #'color-hsl-to-rgb hsl))))
76 (defun jabber-muc-nick-get-color (nick)
78 (let ((color (cdr (assoc nick jabber-muc-participant-colors))))
82 (unless jabber-muc-participant-colors)
83 (push (cons nick (jabber-muc-nick-gen-color nick)) jabber-muc-participant-colors)
84 (cdr (assoc nick jabber-muc-participant-colors))))))
86 (provide 'jabber-muc-nick-coloring)
88 ;;; jabber-muc-nick-coloring.el ends here