* lisp/color.el (color-xyz-to-lab, color-lab-to-xyz, color-cie-de2000):

Prefer pcase-let over destructuring-bind.
* lisp/vc/diff-mode.el (diff-remove-trailing-whitespace): Same.
Also, remove whitespace as we go, rather than after accumulating the
various places.
This commit is contained in:
Stefan Monnier 2012-08-13 15:10:35 -04:00
parent 89660017d1
commit aa7c6dbeba
3 changed files with 123 additions and 123 deletions

View file

@ -1,5 +1,11 @@
2012-08-13 Stefan Monnier <monnier@iro.umontreal.ca>
* color.el (color-xyz-to-lab, color-lab-to-xyz, color-cie-de2000):
Prefer pcase-let over destructuring-bind.
* vc/diff-mode.el (diff-remove-trailing-whitespace): Same.
Also, remove whitespace as we go, rather than after accumulating the
various places.
* subr.el (internal--before-with-selected-window)
(internal--after-with-selected-window): Fix typo seleted->selected.
(with-selected-window): Adjust callers.

View file

@ -1,4 +1,4 @@
;;; color.el --- Color manipulation library -*- coding: utf-8; -*-
;;; color.el --- Color manipulation library -*- coding: utf-8; lexical-binding:t -*-
;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
@ -85,7 +85,7 @@ resulting list."
(g-step (/ (- (nth 1 stop) g) (1+ step-number)))
(b-step (/ (- (nth 2 stop) b) (1+ step-number)))
result)
(dotimes (n step-number)
(dotimes (_ step-number)
(push (list (setq r (+ r r-step))
(setq g (+ g g-step))
(setq b (+ b b-step)))
@ -226,44 +226,44 @@ RED, BLUE and GREEN must be between 0 and 1, inclusive."
"Convert CIE XYZ to CIE L*a*b*.
WHITE-POINT specifies the (X Y Z) white point for the
conversion. If omitted or nil, use `color-d65-xyz'."
(destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz)
(let* ((xr (/ X Xr))
(yr (/ Y Yr))
(zr (/ Z Zr))
(fx (if (> xr color-cie-ε)
(expt xr (/ 1 3.0))
(/ (+ (* color-cie-κ xr) 16) 116.0)))
(fy (if (> yr color-cie-ε)
(expt yr (/ 1 3.0))
(/ (+ (* color-cie-κ yr) 16) 116.0)))
(fz (if (> zr color-cie-ε)
(expt zr (/ 1 3.0))
(/ (+ (* color-cie-κ zr) 16) 116.0))))
(list
(- (* 116 fy) 16) ; L
(* 500 (- fx fy)) ; a
(* 200 (- fy fz)))))) ; b
(pcase-let* ((`(,Xr ,Yr ,Zr) (or white-point color-d65-xyz))
(xr (/ X Xr))
(yr (/ Y Yr))
(zr (/ Z Zr))
(fx (if (> xr color-cie-ε)
(expt xr (/ 1 3.0))
(/ (+ (* color-cie-κ xr) 16) 116.0)))
(fy (if (> yr color-cie-ε)
(expt yr (/ 1 3.0))
(/ (+ (* color-cie-κ yr) 16) 116.0)))
(fz (if (> zr color-cie-ε)
(expt zr (/ 1 3.0))
(/ (+ (* color-cie-κ zr) 16) 116.0))))
(list
(- (* 116 fy) 16) ; L
(* 500 (- fx fy)) ; a
(* 200 (- fy fz))))) ; b
(defun color-lab-to-xyz (L a b &optional white-point)
"Convert CIE L*a*b* to CIE XYZ.
WHITE-POINT specifies the (X Y Z) white point for the
conversion. If omitted or nil, use `color-d65-xyz'."
(destructuring-bind (Xr Yr Zr) (or white-point color-d65-xyz)
(let* ((fy (/ (+ L 16) 116.0))
(fz (- fy (/ b 200.0)))
(fx (+ (/ a 500.0) fy))
(xr (if (> (expt fx 3.0) color-cie-ε)
(expt fx 3.0)
(/ (- (* fx 116) 16) color-cie-κ)))
(yr (if (> L (* color-cie-κ color-cie-ε))
(expt (/ (+ L 16) 116.0) 3.0)
(/ L color-cie-κ)))
(zr (if (> (expt fz 3) color-cie-ε)
(expt fz 3.0)
(/ (- (* 116 fz) 16) color-cie-κ))))
(list (* xr Xr) ; X
(* yr Yr) ; Y
(* zr Zr))))) ; Z
(pcase-let* ((`(,Xr ,Yr ,Zr) (or white-point color-d65-xyz))
(fy (/ (+ L 16) 116.0))
(fz (- fy (/ b 200.0)))
(fx (+ (/ a 500.0) fy))
(xr (if (> (expt fx 3.0) color-cie-ε)
(expt fx 3.0)
(/ (- (* fx 116) 16) color-cie-κ)))
(yr (if (> L (* color-cie-κ color-cie-ε))
(expt (/ (+ L 16) 116.0) 3.0)
(/ L color-cie-κ)))
(zr (if (> (expt fz 3) color-cie-ε)
(expt fz 3.0)
(/ (- (* 116 fz) 16) color-cie-κ))))
(list (* xr Xr) ; X
(* yr Yr) ; Y
(* zr Zr)))) ; Z
(defun color-srgb-to-lab (red green blue)
"Convert RGB to CIE L*a*b*."
@ -277,67 +277,72 @@ conversion. If omitted or nil, use `color-d65-xyz'."
"Return the CIEDE2000 color distance between COLOR1 and COLOR2.
Both COLOR1 and COLOR2 should be in CIE L*a*b* format, as
returned by `color-srgb-to-lab' or `color-xyz-to-lab'."
(destructuring-bind (L a b) color1
(destructuring-bind (L a b) color2
(let* ((kL (or kL 1))
(kC (or kC 1))
(kH (or kH 1))
(C (sqrt (+ (expt a 2.0) (expt b 2.0))))
(C (sqrt (+ (expt a 2.0) (expt b 2.0))))
( (/ (+ C C) 2.0))
(G (* 0.5 (- 1 (sqrt (/ (expt 7.0) (+ (expt 7.0) (expt 25 7.0)))))))
(a (* (+ 1 G) a))
(a (* (+ 1 G) a))
(C (sqrt (+ (expt a 2.0) (expt b 2.0))))
(C (sqrt (+ (expt a 2.0) (expt b 2.0))))
(h (if (and (= b 0) (= a 0))
0
(let ((v (atan b a)))
(if (< v 0)
(+ v (* 2 float-pi))
v))))
(h (if (and (= b 0) (= a 0))
0
(let ((v (atan b a)))
(if (< v 0)
(+ v (* 2 float-pi))
v))))
(ΔL (- L L))
(ΔC (- C C))
(Δh (cond ((= (* C C) 0)
0)
((<= (abs (- h h)) float-pi)
(- h h))
((> (- h h) float-pi)
(- (- h h) (* 2 float-pi)))
((< (- h h) (- float-pi))
(+ (- h h) (* 2 float-pi)))))
(ΔH (* 2 (sqrt (* C C)) (sin (/ Δh 2.0))))
( (/ (+ L L) 2.0))
( (/ (+ C C) 2.0))
( (cond ((= (* C C) 0)
(+ h h))
((<= (abs (- h h)) float-pi)
(/ (+ h h) 2.0))
((< (+ h h) (* 2 float-pi))
(/ (+ h h (* 2 float-pi)) 2.0))
((>= (+ h h) (* 2 float-pi))
(/ (+ h h (* -2 float-pi)) 2.0))))
(T (+ 1
(- (* 0.17 (cos (- (degrees-to-radians 30)))))
(* 0.24 (cos (* 2)))
(* 0.32 (cos (+ (* 3) (degrees-to-radians 6))))
(- (* 0.20 (cos (- (* 4) (degrees-to-radians 63)))))))
(Δθ (* (degrees-to-radians 30) (exp (- (expt (/ (- (degrees-to-radians 275)) (degrees-to-radians 25)) 2.0)))))
(Rc (* 2 (sqrt (/ (expt 7.0) (+ (expt 7.0) (expt 25.0 7.0))))))
(Sl (+ 1 (/ (* 0.015 (expt (- 50) 2.0)) (sqrt (+ 20 (expt (- 50) 2.0))))))
(Sc (+ 1 (* 0.045)))
(Sh (+ 1 (* 0.015 T)))
(Rt (- (* (sin (* Δθ 2)) Rc))))
(pcase-let*
((`(,L ,a ,b) color1)
(`(,L ,a ,b) color2)
(kL (or kL 1))
(kC (or kC 1))
(kH (or kH 1))
(C (sqrt (+ (expt a 2.0) (expt b 2.0))))
(C (sqrt (+ (expt a 2.0) (expt b 2.0))))
( (/ (+ C C) 2.0))
(G (* 0.5 (- 1 (sqrt (/ (expt 7.0)
(+ (expt 7.0) (expt 25 7.0)))))))
(a (* (+ 1 G) a))
(a (* (+ 1 G) a))
(C (sqrt (+ (expt a 2.0) (expt b 2.0))))
(C (sqrt (+ (expt a 2.0) (expt b 2.0))))
(h (if (and (= b 0) (= a 0))
0
(let ((v (atan b a)))
(if (< v 0)
(+ v (* 2 float-pi))
v))))
(h (if (and (= b 0) (= a 0))
0
(let ((v (atan b a)))
(if (< v 0)
(+ v (* 2 float-pi))
v))))
(ΔL (- L L))
(ΔC (- C C))
(Δh (cond ((= (* C C) 0)
0)
((<= (abs (- h h)) float-pi)
(- h h))
((> (- h h) float-pi)
(- (- h h) (* 2 float-pi)))
((< (- h h) (- float-pi))
(+ (- h h) (* 2 float-pi)))))
(ΔH (* 2 (sqrt (* C C)) (sin (/ Δh 2.0))))
( (/ (+ L L) 2.0))
( (/ (+ C C) 2.0))
( (cond ((= (* C C) 0)
(+ h h))
((<= (abs (- h h)) float-pi)
(/ (+ h h) 2.0))
((< (+ h h) (* 2 float-pi))
(/ (+ h h (* 2 float-pi)) 2.0))
((>= (+ h h) (* 2 float-pi))
(/ (+ h h (* -2 float-pi)) 2.0))))
(T (+ 1
(- (* 0.17 (cos (- (degrees-to-radians 30)))))
(* 0.24 (cos (* 2)))
(* 0.32 (cos (+ (* 3) (degrees-to-radians 6))))
(- (* 0.20 (cos (- (* 4) (degrees-to-radians 63)))))))
(Δθ (* (degrees-to-radians 30)
(exp (- (expt (/ (- (degrees-to-radians 275))
(degrees-to-radians 25)) 2.0)))))
(Rc (* 2 (sqrt (/ (expt 7.0) (+ (expt 7.0) (expt 25.0 7.0))))))
(Sl (+ 1 (/ (* 0.015 (expt (- 50) 2.0))
(sqrt (+ 20 (expt (- 50) 2.0))))))
(Sc (+ 1 (* 0.045)))
(Sh (+ 1 (* 0.015 T)))
(Rt (- (* (sin (* Δθ 2)) Rc))))
(sqrt (+ (expt (/ ΔL (* Sl kL)) 2.0)
(expt (/ ΔC (* Sc kC)) 2.0)
(expt (/ ΔH (* Sh kH)) 2.0)
(* Rt (/ ΔC (* Sc kC)) (/ ΔH (* Sh kH)))))))))
(* Rt (/ ΔC (* Sc kC)) (/ ΔH (* Sh kH)))))))
(defun color-clamp (value)
"Make sure VALUE is a number between 0.0 and 1.0 inclusive."

View file

@ -2024,37 +2024,26 @@ with the name of the altered buffers, which are unsaved. If a
file referenced on the diff has no buffer and needs to be fixed,
a buffer visiting that file is created."
(interactive)
(goto-char (point-min))
(let
;; We assume that the diff header has no trailing whitespace.
((modified-buffers nil)
(white-positions nil))
(while (re-search-forward "^[+!>].*[ \t]+$" (point-max) t)
(save-excursion
(cl-destructuring-bind (buf line-offset pos src _dst &optional _switched)
(diff-find-source-location t t)
(when line-offset
(set-buffer buf)
(save-excursion
(goto-char (+ (car pos) (cdr src)))
(beginning-of-line)
(when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t)
(when (not (member buf modified-buffers))
(push buf modified-buffers))
(goto-char (match-end 0))
(push (point-marker) white-positions)
(goto-char (match-beginning 0))
(push (point-marker) white-positions)
(push buf white-positions)))))))
(while white-positions
(save-excursion
(set-buffer (pop white-positions))
(delete-region (pop white-positions) (pop white-positions))))
;; We assume that the diff header has no trailing whitespace.
(let ((modified-buffers nil))
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^[+!>].*[ \t]+$" (point-max) t)
(pcase-let ((`(,buf ,line-offset ,pos ,src ,_dst ,_switched)
(diff-find-source-location t t)))
(when line-offset
(with-current-buffer buf
(save-excursion
(goto-char (+ (car pos) (cdr src)))
(beginning-of-line)
(when (re-search-forward "\\([ \t]+\\)$" (line-end-position) t)
(unless (memq buf modified-buffers)
(push buf modified-buffers))
(replace-match ""))))))))
(if modified-buffers
(let ((msg "Deleted new trailing whitespace from:"))
(dolist (f modified-buffers)
(setq msg (concat msg " `" (buffer-name f) "'")))
(message "%s" msg))
(message "Deleted new trailing whitespace from: %s"
(mapconcat (lambda (buf) (concat "`" (buffer-name buf) "'"))
modified-buffers " "))
(message "No trailing whitespace fixes needed."))))
;; provide the package