├── .gitignore ├── ColorBar.m └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.nb 2 | *.iml 3 | .idea/ 4 | -------------------------------------------------------------------------------- /ColorBar.m: -------------------------------------------------------------------------------- 1 | (* ColorFunction designer for Mathematica *) 2 | (* :Context: ColorBar` *) 3 | (* :Author: rsmenon *) 4 | (* :License: MIT *) 5 | (* :Date: February 24, 2014 *) 6 | 7 | BeginPackage["ColorBar`"] 8 | 9 | ColorBar::usage = "ColorBar[\"Named Gradient or ColorFunction\"] will open up a ColorFunction designer for the specified color function or gradient." 10 | 11 | Begin["`Private`"] 12 | 13 | Unprotect@ColorBar 14 | ColorBar[cf : _String | _ColorDataFunction | _Function] := 15 | DynamicModule[ 16 | { 17 | control, addControl, removeControl, controlPoints, defaultControlPoints, getControlPoints, 18 | controlColors, defaultControlColors, colorData, colorDataList, colorFunction, defaultColorFunction, 19 | getColorFunction, updateColorFunction, generateColorFunction, colorbar, triangle, 20 | getXPosition, initializeSettings, local = "ColorBar`Private`local" 21 | }, 22 | 23 | colorDataList = ColorData["Gradients"]; 24 | triangle = Polygon[{{# - 0.025, 0}, {#, -0.025 Sqrt[3]}, {# + 0.025, 0}}] &; 25 | colorbar = Raster[{Range[0, 100] / 100}, {{0, 0}, {1, 0.1}}, ColorFunction -> #] &; 26 | colorData = Switch[cf, _String, cf, _ColorDataFunction, First@cf, _Function, "Custom"]; 27 | 28 | getColorFunction[cd_] := Switch[cd, "Custom", colorDataList = colorDataList ~Join~ {cd};cf, _, ColorData[cd]]; 29 | 30 | getControlPoints[cd_] := 31 | Block[{func}, 32 | func[c : {__List}] := c[[All, 1]]; 33 | func[c_List] := Array[# &, Length@c, {0, 1.}]; 34 | func[___] := Array[# &, 5, {0, 1.}]; 35 | 36 | Switch[cd, 37 | "Custom", 38 | defaultColorFunction /. HoldPattern[Function[___, Blend[x_, _], ___]] :> func@x, 39 | _, 40 | func@DataPaclets`ColorDataDump`getColorSchemeData[colorData][[5]] 41 | ] 42 | ]; 43 | 44 | initializeSettings = 45 | Function[x, 46 | colorData = x; 47 | defaultColorFunction = getColorFunction@colorData; 48 | colorFunction = defaultColorFunction; 49 | 50 | defaultControlPoints = getControlPoints@colorData; 51 | controlPoints = Hold /@ Table[Unique@local, {Length@defaultControlPoints}]; 52 | Evaluate[ReleaseHold@controlPoints] = defaultControlPoints; 53 | 54 | defaultControlColors = defaultColorFunction /@ defaultControlPoints; 55 | controlColors = Hold /@ Table[Unique@local, {Length@defaultControlColors}]; 56 | Evaluate[ReleaseHold@controlColors] = defaultControlColors; 57 | ]; 58 | 59 | initializeSettings[colorData]; 60 | 61 | getXPosition[] := With[{mp = MousePosition["Graphics"]}, If[mp === None, $Failed, Clip[First@mp, {0,1}]]]; 62 | 63 | SetAttributes[addControl, HoldAll]; 64 | addControl[$Failed] = Null; 65 | addControl[pos_] := With[{symP = Unique@local, symC = Unique@local}, 66 | AppendTo[controlPoints, Hold@symP]; 67 | AppendTo[controlColors, Hold@symC]; 68 | symP = pos; 69 | symC = colorFunction[pos]; 70 | ]; 71 | 72 | SetAttributes[removeControl, HoldAll]; 73 | removeControl[$Failed] = Null; 74 | removeControl[pos_] := ({controlPoints, controlColors} = 75 | Transpose@{controlPoints, controlColors} /. {Hold[pos], _} :> Sequence[] // Transpose); 76 | 77 | 78 | generateColorFunction[pts_, cols_] := Blend[Transpose@{pts, cols}, #] &; 79 | 80 | updateColorFunction[] := colorFunction = generateColorFunction[ReleaseHold@controlPoints, ReleaseHold@controlColors]; 81 | 82 | SetAttributes[control, HoldAll]; 83 | control[pos_, color_] := 84 | EventHandler[ 85 | Dynamic@{FaceForm@color, EdgeForm[{Thin, Black}], triangle@pos}, 86 | { 87 | "MouseClicked" :> If[ 88 | CurrentValue["ShiftKey"], 89 | 90 | If[Length@controlPoints <= 2, 91 | Null, 92 | removeControl[pos]; 93 | updateColorFunction[]; 94 | ], 95 | 96 | color = If[# === $Canceled, color, #] &@SystemDialogInput["Color", color]; updateColorFunction[] 97 | ], 98 | "MouseDragged" :> (pos = If[# === $Failed, pos, #] &@getXPosition[]; updateColorFunction[]) 99 | }, 100 | Method -> "Queued" 101 | ]; 102 | 103 | Interpretation[ 104 | Deploy@Panel@Column[{ 105 | PopupMenu[Dynamic[colorData, initializeSettings], colorDataList], 106 | 107 | Column[{ 108 | EventHandler[ 109 | Graphics[ 110 | Dynamic[control @@@ Transpose[{controlPoints, controlColors}] // ReleaseHold], 111 | PlotRange -> {{0, 1}, All}, PlotRangePadding -> 0, 112 | ImageSize -> 300, ImagePadding -> {{10, 10}, {0, 0}} 113 | ], 114 | {"MouseClicked" :> If[CurrentValue["CommandKey"], addControl[getXPosition[]]]}, 115 | Method -> "Queued" 116 | ], 117 | 118 | Dynamic@Graphics[ 119 | colorbar@generateColorFunction[ReleaseHold@controlPoints, ReleaseHold@controlColors], 120 | PlotRange -> {{0, 1}, All}, PlotRangePadding -> 0, ImageSize -> 300, ImagePadding -> {{10, 10}, {15, 0}}, 121 | Frame -> True, FrameTicks -> {True, False, False, False}, FrameTicksStyle -> Directive[FontSize -> 12] 122 | ] 123 | }] 124 | }], 125 | Dynamic@generateColorFunction[ReleaseHold@controlPoints, ReleaseHold@controlColors] 126 | ] 127 | ] 128 | 129 | ColorBar[] := ColorBar[First@ColorData["Gradients"]] 130 | 131 | SetAttributes[ColorBar, {Protected, ReadProtected}]; 132 | End[] 133 | 134 | EndPackage[] 135 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > ## ⚠️ THIS PACKAGE IS NOT MAINTAINED 2 | 3 | ColorBar 4 | ======== 5 | 6 | `ColorBar` is an interactive `ColorFunction` designer for _Mathematica_. It allows you to easily modify existing color functions, change colors or vary the blending fraction and get the final result in other applications. You can also use it directly inside a plotting function. 7 | 8 | ### Installation & basic usage 9 | Copy the `ColorBar.m` file to your `$UserBaseDirectory` and load it in via ``Needs["ColorBar`"]``. The `ColorBar` function can be used with built-in color functions 10 | 11 | ![ColorBar usage: built-in](https://f.cloud.github.com/assets/2389211/2275910/4502c83e-9f2d-11e3-83d2-4117696028f2.png) 12 | 13 | or with custom color functions 14 | 15 | ![ColorBar usage: custom](https://f.cloud.github.com/assets/2389211/2266386/9821c60a-9e97-11e3-9624-f348da7686be.png) 16 | 17 | Use `Setting` to extract the corresponding color function. 18 | 19 | ![Setting](https://f.cloud.github.com/assets/2389211/2275909/45024cce-9f2d-11e3-8b1d-093d1fa80efb.png) 20 | 21 | Use "Evaluate in place" on `ColorBar[]` to use the colorbar designer inside a plotting function. 22 | 23 | ### Modifying control points 24 | 25 | - Click and drag the control points (triangles) to change the transition region 26 | 27 | - Click on a control point to change its color 28 | 29 | - Click while holding down Command (or Alt in Windows & Linux) to add a control point at that location. 30 | 31 | - Click on a control point while holding Shift to delete a control point (a minimum of 2 control points will always remain). 32 | --------------------------------------------------------------------------------