Commit aa7c6dbe authored by Stefan Monnier's avatar Stefan Monnier
Browse files

* 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.
parent 89660017
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.
......
;;; 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 C) 2.0))
(G (* 0.5 (- 1 (sqrt (/ (expt C̄ 7.0) (+ (expt C̄ 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 L) 2.0))
(C̄′ (/ (+ C′₁ C′₂) 2.0))
(h̄′ (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 (- h̄′ (degrees-to-radians 30)))))
(* 0.24 (cos (* h̄′ 2)))
(* 0.32 (cos (+ (* h̄′ 3) (degrees-to-radians 6))))
(- (* 0.20 (cos (- (* h̄′ 4) (degrees-to-radians 63)))))))
(Δθ (* (degrees-to-radians 30) (exp (- (expt (/ (- h̄′ (degrees-to-radians 275)) (degrees-to-radians 25)) 2.0)))))
(Rc (* 2 (sqrt (/ (expt C̄′ 7.0) (+ (expt C̄′ 7.0) (expt 25.0 7.0))))))
(Sl (+ 1 (/ (* 0.015 (expt (- L̄′ 50) 2.0)) (sqrt (+ 20 (expt (- L̄′ 50) 2.0))))))
(Sc (+ 1 (* C̄′ 0.045)))
(Sh (+ 1 (* 0.015 C̄′ 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 C) 2.0))
(G (* 0.5 (- 1 (sqrt (/ (expt C̄ 7.0)
(+ (expt C̄ 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 L) 2.0))
(C̄′ (/ (+ C′₁ C′₂) 2.0))
(h̄′ (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 (- h̄′ (degrees-to-radians 30)))))
(* 0.24 (cos (* h̄′ 2)))
(* 0.32 (cos (+ (* h̄′ 3) (degrees-to-radians 6))))
(- (* 0.20 (cos (- (* h̄′ 4) (degrees-to-radians 63)))))))
(Δθ (* (degrees-to-radians 30)
(exp (- (expt (/ (- h̄′ (degrees-to-radians 275))
(degrees-to-radians 25)) 2.0)))))
(Rc (* 2 (sqrt (/ (expt C̄′ 7.0) (+ (expt C̄′ 7.0) (expt 25.0 7.0))))))
(Sl (+ 1 (/ (* 0.015 (expt (- L̄′ 50) 2.0))
(sqrt (+ 20 (expt (- L̄′ 50) 2.0))))))
(Sc (+ 1 (* C̄′ 0.045)))
(Sh (+ 1 (* 0.015 C̄′ 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."
......
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment