├── .gitignore ├── LICENSE ├── README.org ├── assets └── presets │ ├── blue-cyan.svg │ ├── blue-magenta-orange.svg │ ├── blue-white-red.svg │ ├── cyan-magenta.svg │ ├── green-blue-orange.svg │ ├── green-cyan.svg │ ├── green-magenta.svg │ ├── green-red.svg │ ├── magenta-green.svg │ ├── orange-blue.svg │ ├── orange-magenta-blue.svg │ ├── rainbow1.svg │ ├── rainbow2.svg │ ├── rainbow3.svg │ ├── rainbow4.svg │ ├── red-blue.svg │ ├── yellow-green-blue.svg │ ├── yellow-magenta-cyan.svg │ ├── yellow-purple-magenta.svg │ └── yellow-red.svg ├── src ├── core.org ├── gradients.org ├── libraryofbabel.org ├── presets.org └── setup.org ├── tangle-all.sh ├── tangle.sh └── test └── core.org /.gitignore: -------------------------------------------------------------------------------- 1 | babel 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /assets/presets/blue-cyan.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/blue-magenta-orange.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/blue-white-red.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/cyan-magenta.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/green-cyan.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/magenta-green.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/orange-magenta-blue.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/rainbow1.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/rainbow2.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/rainbow3.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/rainbow4.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/yellow-green-blue.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/yellow-magenta-cyan.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/yellow-purple-magenta.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /assets/presets/yellow-red.svg: -------------------------------------------------------------------------------- 1 | 2 | 0.000.501.000.000.200.400.600.801.00 -------------------------------------------------------------------------------- /src/gradients.org: -------------------------------------------------------------------------------- 1 | #+SETUPFILE: setup.org 2 | 3 | * Contents :toc_3_gh: 4 | - [[#namespace-thingcolorgradients][Namespace: thi.ng.color.gradients]] 5 | - [[#cosine-based-gradient-generation][Cosine based gradient generation]] 6 | - [[#presets][Presets]] 7 | - [[#implementation][Implementation]] 8 | - [[#gradient-coefficient-calculation][Gradient coefficient calculation]] 9 | - [[#example-usage][Example usage]] 10 | - [[#gradient-presets-visualization][Gradient presets visualization]] 11 | - [[#complete-namespace-definition][Complete namespace definition]] 12 | 13 | * Namespace: thi.ng.color.gradients 14 | 15 | ** Cosine based gradient generation 16 | 17 | Based on method by developed iq (Iñigo Quílez): http://v.gd/B2aySt 18 | 19 | Online gradient designer / editor: http://dev.thi.ng/gradients/ 20 | 21 | *** Presets 22 | 23 | Here we define a number of RGB gradient presets, primarily useful for 24 | data visualization purposes: 25 | 26 | #+BEGIN_SRC clojure :noweb-ref generators 27 | (def cosine-schemes 28 | {:rainbow1 [[0.5 0.5 0.5] [0.5 0.5 0.5] [1.0 1.0 1.0] [0 0.3333 0.6666]] 29 | :rainbow2 [[0.5 0.5 0.5] [0.666 0.666 0.666] [1.0 1.0 1.0] [0 0.3333 0.6666]] 30 | :rainbow3 [[0.5 0.5 0.5] [0.75 0.75 0.75] [1.0 1.0 1.0] [0 0.3333 0.6666]] 31 | :rainbow4 [[0.5 0.5 0.5] [1 1 1] [1.0 1.0 1.0] [0 0.3333 0.6666]] 32 | :yellow-magenta-cyan [[1 0.5 0.5] [0.5 0.5 0.5] [0.75 1.0 0.6666] [0.8 1.0 0.3333]] 33 | :orange-blue [[0.5 0.5 0.5] [0.5 0.5 0.5] [0.8 0.8 0.5] [0 0.2 0.5]] 34 | :green-magenta [[0.6666 0.5 0.5] [0.5 0.6666 0.5] [0.6666 0.666 0.5] [0.2 0.0 0.5]] 35 | :green-red [[0.5 0.5 0] [0.5 0.5 0] [0.5 0.5 0] [0.5 0.0 0]] 36 | :green-cyan [[0.0 0.5 0.5] [0 0.5 0.5] [0.0 0.3333 0.5] [0.0 0.6666 0.5]] 37 | :yellow-red [[0.5 0.5 0] [0.5 0.5 0] [0.1 0.5 0] [0.0 0.0 0]] 38 | :blue-cyan [[0.0 0.5 0.5] [0 0.5 0.5] [0.0 0.5 0.3333] [0.0 0.5 0.6666]] 39 | :red-blue [[0.5 0 0.5] [0.5 0 0.5] [0.5 0 0.5] [0 0 0.5]] 40 | :yellow-green-blue [[0.650 0.5 0.310] [-0.650 0.5 0.6] [0.333 0.278 0.278] [0.660 0.0 0.667]] 41 | :blue-white-red [[0.660 0.56 0.680] [0.718 0.438 0.720] [0.520 0.8 0.520] [-0.430 -0.397 -0.083]] 42 | :cyan-magenta [[0.610 0.498 0.650] [0.388 0.498 0.350] [0.530 0.498 0.620] [3.438 3.012 4.025]] 43 | :yellow-purple-magenta [[0.731 1.098 0.192] [0.358 1.090 0.657] [1.077 0.360 0.328] [0.965 2.265 0.837]] 44 | :green-blue-orange [[0.892 0.725 0.000] [0.878 0.278 0.725] [0.332 0.518 0.545] [2.440 5.043 0.732]] 45 | :orange-magenta-blue [[0.821 0.328 0.242] [0.659 0.481 0.896] [0.612 0.340 0.296] [2.820 3.026 -0.273]] 46 | :blue-magenta-orange [[0.938 0.328 0.718] [0.659 0.438 0.328] [0.388 0.388 0.296] [2.538 2.478 0.168]] 47 | :magenta-green [[0.590 0.811 0.120] [0.410 0.392 0.590] [0.940 0.548 0.278] [-4.242 -6.611 -4.045]]}) 48 | #+END_SRC 49 | 50 | Diagrams of the presets showing their channel curves and resulting 51 | gradients (using 100 samples): 52 | 53 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/rainbow1.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/rainbow2.svg]] | 54 | | :rainbow1 | :rainbow2 | 55 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/rainbow3.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/rainbow4.svg]] | 56 | | :rainbow3 | :rainbow4 | 57 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/yellow-magenta-cyan.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/orange-blue.svg]] | 58 | | :yellow-magenta-cyan preset | :orange-blue | 59 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/green-magenta.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/green-red.svg]] | 60 | | :green-magenta | :green-red | 61 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/green-cyan.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/blue-cyan.svg]] | 62 | | :green-cyan | :blue-cyan | 63 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/yellow-red.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/red-blue.svg]] | 64 | | :yellow-red | :red-blue | 65 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/yellow-green-blue.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/blue-white-red.svg]] | 66 | | :yellow-green-blue | :blue-white-red | 67 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/cyan-magenta.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/yellow-purple-magenta.svg]] | 68 | | :cyan-magenta | :yellow-purple-magenta | 69 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/green-blue-orange.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/orange-magenta-blue.svg]] | 70 | | :green-blue-orange | :orange-magenta-blue | 71 | | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/blue-magenta-orange.svg]] | [[https://raw.githubusercontent.com/thi-ng/color/refs/heads/master/assets/presets/magenta-green.svg]] | 72 | | :blue-magenta-orange | :magenta-green | 73 | 74 | *** Implementation 75 | 76 | The two functions below implement the gradient generation function: 77 | =cosine-gradient-color= to compute single colors and 78 | =cosine-gradient-scheme= to create a vector of n tuples covering the 79 | full gradient range. 80 | 81 | *Note:* These function are not restricted to RGB colors and can be 82 | used in many other contexts - think of it as an ND waveform 83 | generator... 84 | 85 | #+BEGIN_SRC clojure :noweb-ref generators 86 | (defn cosine-gradient-color 87 | [offset amp fmod phase t] 88 | (col/rgba 89 | (mapv 90 | (fn [a b c d] (m/clamp (+ a (* b (Math/cos (* TWO_PI (+ (* c t) d))))) 0 1)) 91 | offset amp fmod phase))) 92 | 93 | (defn cosine-gradient 94 | "Takes a length n and 4 cosine coefficients (for colors usually 3 or 95 | 4-element vectors) and produces vector of n new RGBA colors, with 96 | each of its elements defined by an AM & FM cosine wave and clamped 97 | to the [0 1] interval. The fn `t` is used to ramp the gradient and 98 | remap the interpolation interval (e.g. use m/mix-circular)" 99 | ([n spec] 100 | (apply cosine-gradient n m/mix* spec)) 101 | ([n t spec] 102 | (apply cosine-gradient n t spec)) 103 | ([n t offset amp fmod phase] 104 | (mapv #(cosine-gradient-color offset amp fmod phase (t 0 1 %)) (m/norm-range (dec n))))) 105 | #+END_SRC 106 | 107 | *** Gradient coefficient calculation 108 | 109 | Based on [[https://gist.github.com/dimovich/bc52b77e2280ac56a2ed68a987d5ec57][@dimovich's gist]], this function computes the cosine gradient 110 | coefficients for a gradient between two given colors. 111 | 112 | #+BEGIN_SRC clojure :noweb-ref generators 113 | (defn cosine-coefficients 114 | "Computes coefficients defining a cosine gradient between 115 | the two given colors. The colors can be in any color space, 116 | but the resulting gradient will always be computed in RGB. 117 | 118 | amp = (R1 - R2) / 2 119 | dc = R1 - amp 120 | freq = -0.5" 121 | ([c1 c2] 122 | (let [colors (map #(select-keys (col/as-rgba %) [:r :g :b]) [c1 c2]) 123 | amp (apply mapv (fn [[_ v1] [_ v2]] (* 0.5 (- v1 v2))) colors) 124 | offset (mapv (fn [[_ v1] a] (- v1 a)) (first colors) amp)] 125 | [offset 126 | amp 127 | [-0.500 -0.500 -0.500] 128 | [0.000 0.000 0.000]]))) 129 | #+END_SRC 130 | 131 | *** Example usage 132 | 133 | #+BEGIN_SRC clojure 134 | ;; using a preset 135 | (def my-grad 136 | (->> :red-blue 137 | (grad/cosine-schemes) 138 | (grad/cosine-gradient 100))) 139 | 140 | ;; specifying cosine coefficients directly 141 | (def my-grad 142 | (grad/cosine-gradient 100 [0.5 0 0.5] [0.5 0 0.5] [0.5 0 0.5] [0 0 0.5])) 143 | #+END_SRC 144 | 145 | *** Gradient presets visualization 146 | 147 | This following snippet is *not* part of the library and only used to 148 | create the above preset visualizations. Requires http://thi.ng/geom to 149 | be added to your REPL/project in order to run... 150 | 151 | When tangling this file, the code below will be saved in the 152 | =/babel/dev/= subdir of this project... 153 | 154 | #+BEGIN_SRC clojure :tangle ../babel/dev/cosine-previews.clj :mkdirp yes :padline no 155 | (require '[thi.ng.color.core :as col]) 156 | (require '[thi.ng.color.gradients :as grad]) 157 | (require '[thi.ng.geom.viz.core :as viz]) 158 | (require '[thi.ng.geom.svg.core :as svg]) 159 | (require '[thi.ng.math.core :as m]) 160 | 161 | (defn channel-specs 162 | [colors] 163 | (map-indexed 164 | (fn [idx col] 165 | {:values (viz/uniform-domain-points [0 1] (map #(nth (deref %) idx) colors)) 166 | :attribs {:fill "none" :stroke col} 167 | :layout viz/svg-line-plot}) 168 | ["red" "green" "blue"])) 169 | 170 | (defn color-bars 171 | [x1 x2 y w h colors] 172 | (let [n (dec (count colors))] 173 | (for [i (m/norm-range n)] 174 | (svg/rect [(m/mix* x1 x2 i) y] w h {:fill (colors (int (* i n)))})))) 175 | 176 | (doseq [[id coeffs] grad/cosine-schemes] 177 | (let [colors (grad/cosine-gradient 100 coeffs)] 178 | (->> {:x-axis (viz/linear-axis 179 | {:domain [0 1] :range [50 580] :major 0.5 :minor 0.125 :pos 250}) 180 | :y-axis (viz/linear-axis 181 | {:domain [0 1] :range [250 20] :major 0.2 :minor 0.1 :pos 50 182 | :label-dist 15 :label-style {:text-anchor "end"}}) 183 | :grid {:minor-x true :minor-y true} 184 | :data (channel-specs colors)} 185 | (viz/svg-plot2d-cartesian) 186 | (svg/svg 187 | {:width 600 :height 300} 188 | (color-bars 50 570 280 10 20 colors)) 189 | (svg/serialize) 190 | (spit (str (name id) ".svg"))))) 191 | #+END_SRC 192 | 193 | ** Complete namespace definition 194 | 195 | #+BEGIN_SRC clojure :tangle ../babel/src/thi/ng/color/gradients.cljc :noweb yes :mkdirp yes :padline no 196 | (ns thi.ng.color.gradients 197 | #?(:cljs 198 | (:require-macros 199 | [thi.ng.math.macros :as mm])) 200 | (:require 201 | [thi.ng.math.core :as m :refer [PI TWO_PI]] 202 | [thi.ng.color.core :as col] 203 | #?(:clj [thi.ng.math.macros :as mm]))) 204 | 205 | <> 206 | #+END_SRC 207 | -------------------------------------------------------------------------------- /src/libraryofbabel.org: -------------------------------------------------------------------------------- 1 | #+SETUPFILE: setup.org 2 | 3 | * The common Library of Babel 4 | 5 | This file contains shared and configurable code templates for various 6 | parts of this project. Templates using variables are wrapped in a 7 | simple Elisp form, but you can also define verbatim templates if vars 8 | aren't needed... 9 | 10 | ** Example template with variables 11 | #+NAME: lob-common-greetings 12 | #+BEGIN_SRC emacs-lisp :var msg="" 13 | (replace-regexp-in-string 14 | "~~msg~~" msg 15 | "(defn show-greetings 16 | [] (println \"~~msg~~\"))") 17 | #+END_SRC 18 | 19 | ** Example verbatim template 20 | #+NAME: lob-common-verbatim 21 | #+BEGIN_SRC clojure 22 | (* 12345679 7.2) 23 | #+END_SRC 24 | -------------------------------------------------------------------------------- /src/setup.org: -------------------------------------------------------------------------------- 1 | #+SEQ_TODO: TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@) 2 | #+TAGS: write(w) fix(f) verify(v) noexport(n) template(t) usetemplate(u) 3 | #+EXPORT_EXCLUDE_TAGS: noexport 4 | #+AUTHOR: Karsten Schmidt 5 | #+EMAIL: k@thi.ng 6 | #+LANGUAGE: en 7 | #+OPTIONS: toc:3 h:4 html-postamble:auto html-preamble:t tex:t 8 | #+HTML_CONTAINER: div 9 | #+HTML_DOCTYPE: 10 | #+HTML_HEAD: 11 | #+HTML_HEAD: 12 | -------------------------------------------------------------------------------- /tangle-all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | FILES="README.org" 4 | 5 | SRC="src/*.org test/*.org" 6 | rm -rf babel/src babel/test 7 | for f in `ls $SRC`; do 8 | FILES="$FILES $f" 9 | done 10 | 11 | ./tangle.sh $FILES 12 | -------------------------------------------------------------------------------- /tangle.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | DIR=`pwd` 4 | FILES="" 5 | 6 | # wrap each argument in the code required to call tangle on it 7 | for i in $@; do 8 | FILES="$FILES \"$i\"" 9 | done 10 | 11 | emacs -Q --batch \ 12 | --eval \ 13 | "(progn 14 | (require 'org)(require 'ob)(require 'ob-tangle)(require 'ob-lob) 15 | (org-babel-lob-ingest \"src/libraryofbabel.org\") 16 | (setq org-confirm-babel-evaluate nil) 17 | (mapc (lambda (file) 18 | (find-file (expand-file-name file \"$DIR\")) 19 | (org-babel-tangle) 20 | (kill-buffer)) '($FILES)))" \ 21 | #2>&1 | grep Tangled 22 | --------------------------------------------------------------------------------