Skip to content

Commit e83ab36

Browse files
committed
resize pixels, pack colors, reduce colors modified
1 parent aaaff64 commit e83ab36

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# Change Log
22
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
33

4+
## 1.0.2
5+
6+
### Added
7+
8+
* `pack` color into 32 bit integer
9+
* `resize` Pixels
10+
11+
### Changed
12+
13+
* `reduce-colors` - selects color from cluster using `mode` rather than centroid
14+
415
## 1.0.1
516

617
### Added

example/clojure2d/color_example.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@
125125
(add-examples format-hex
126126
(example-session "Usage" (format-hex :maroon) (format-hex (color 4 55 222))))
127127

128+
(add-examples pack
129+
(example-session "Pack colors into 32 bit int"
130+
(pack :green)
131+
(pack 1233456)
132+
(pack 0x1122ff)
133+
(pack (to-color 0x1122ff))
134+
(pack (color 12 33 255))))
135+
128136
;; -- thing
129137

130138
(add-examples to-thing-rgba

generateme/colors.clj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
(:require [clojure2d.core :refer :all]
33
[clojure2d.color :as c]
44
[fastmath.core :as m]
5-
[fastmath.vector :as v]))
5+
[fastmath.vector :as v]
6+
[clojure2d.extra.utils :as u]
7+
[fastmath.clustering :as cl]
8+
[clojure2d.pixels :as p]
9+
[fastmath.stats :as stat]))
610

711
(def canv (canvas 800 300))
812

@@ -18,3 +22,17 @@
1822
(line c x 0 x 150)
1923
(set-color c (p1 t))
2024
(line c x 150 x 300)))))
25+
26+
;;
27+
28+
(def img (p/load-pixels "docs/samurai.jpg"))
29+
30+
(u/show-palette (c/reduce-colors :LUV (resize (p/load-pixels "bbb1.jpg") 600 600) 5) )
31+
32+
;;
33+
34+
(save (:buffer (u/show-gradient (c/gradient :LAB :cubic-spline (c/reduce-colors :LAB (resize (p/load-pixels "b5.jpg") 600 600) 5)))) "b5grad.jpg")
35+
36+
(u/show-gradient (c/gradient :LAB :cubic-spline (c/reduce-colors :LUV (resize (p/load-pixels "bbb1.jpg") 600 600) 4)))
37+
38+
;;

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject clojure2d "1.0.1"
1+
(defproject clojure2d "1.0.2"
22
:description "Creative coding / glitch library backed by Java2D"
33
:url "https://github.com/Clojure2D/clojure2d"
44
:license {:name "MIT"

src/clojure2d/color.clj

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,14 @@
459459
[c]
460460
(str "#" (format "%02x" (lclamp255 (red c))) (format "%02x" (lclamp255 (green c))) (format "%02x" (lclamp255 (blue c)))))
461461

462+
(defn pack
463+
"Pack color to ARGB 32bit integer."
464+
[c]
465+
(unchecked-int (bit-or (<< (lclamp255 (alpha c)) 24)
466+
(<< (lclamp255 (red c)) 16)
467+
(<< (lclamp255 (green c)) 8)
468+
(lclamp255 (blue c)))))
469+
462470
;; ---------- blending
463471

464472
(def ^:dynamic ^{:metadoc/categories #{:bl} :doc "Some blend functions can be parametrized with `threshold` value. Default `0.5`."} ^double *blend-threshold* 0.5)
@@ -2549,14 +2557,19 @@ See [[blends-list]] for names."}
25492557
(defn reduce-colors
25502558
"Reduce colors using x-means clustering in given `colorspace` (default `:RGB`).
25512559
2552-
Use for long sequences."
2553-
([xs number-of-colors]
2554-
(sort-by luma (for [{:keys [representative]} (cl/regroup (cl/x-means xs number-of-colors))]
2555-
representative)))
2560+
Use for long sequences (for example to generate palette from image)."
2561+
([xs number-of-colors] (reduce-colors :RGB xs number-of-colors))
25562562
([colorspace xs number-of-colors]
25572563
(let [[to from] (colorspaces* colorspace)]
2558-
(sort-by luma (for [{:keys [representative]} (cl/regroup (cl/x-means (map (comp to to-color) xs) number-of-colors))]
2559-
(from representative))))))
2564+
(sort-by luma ;; sort by brightness
2565+
(for [{:keys [data]} (-> (map to xs) ;; convert to given colorspace
2566+
(cl/x-means number-of-colors) ;; clustering
2567+
(cl/regroup))] ;; reshape
2568+
(->> (map pack data) ;; pack colors into integers
2569+
(stat/modes) ;; find colors which appears most often
2570+
(map (comp to-color unchecked-long)) ;; convert back to colors
2571+
(v/average-vectors) ;; average vectors if necessary
2572+
(from))))))) ;; convert back to RGB
25602573

25612574
;; colors
25622575

@@ -2759,7 +2772,7 @@ See [[blends-list]] for names."}
27592772
27602773
Grandient function accepts value from 0 to 1 and returns interpolated color.
27612774
2762-
Optionally interpolate in given `colorspace` and 1d `interpolator` as keyword or function.
2775+
Optionally interpolate in given `colorspace` and 1d [interpolator](https://generateme.github.io/fastmath/fastmath.interpolation.html#var-interpolators-1d-list) as keyword or function.
27632776
27642777
To make irregular spacings between colors, provide own `domain`."
27652778
{:metadoc/categories #{:grad}}

src/clojure2d/pixels.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
px)
175175
(convolve [_ t]
176176
(core/convolve (clojure2d.java.Pixels/imageFromPixels p w h) t))
177+
(resize [_ wi he] (to-pixels (core/resize (clojure2d.java.Pixels/imageFromPixels p w h) wi he)))
177178

178179
Counted
179180
(count [_] size)

0 commit comments

Comments
 (0)