Skip to content

Commit

Permalink
add to docs, improve CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
cormullion committed Aug 28, 2024
1 parent ac5902a commit d801a9f
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 32 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ colorschemes

findcolorscheme("purple")
# => display list of matching schemes

ColorScheme([colorant"red", colorant"green", colorant"blue"])
# new colorscheme from Colors.jl named colors

get(ColorSchemes.darkrainbow, range(0.0, 1.0, length=20)) |> ColorScheme
# new colorscheme by resampling existing
```

[docs-current-img]: https://img.shields.io/badge/docs-current-blue.svg
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ makedocs(
"Finding colors" => "finding.md",
"Plotting" => "plotting.md",
"Images" => "images.md",
"Index" => "functionindex.md"
"Functions" => "functionindex.md"
]
)

Expand Down
15 changes: 12 additions & 3 deletions docs/src/assets/colorschemes-docs.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
--lessdark: hsl(237, 20%, 16%);
}


html.theme--documenter-dark body {
background-color: var(--verydark);
color: #eff;
Expand Down Expand Up @@ -83,8 +84,16 @@ html.theme--documenter-dark .modal-card-head {
font-family: "JuliaMono";
}

/*
is this how to scale the SVGs to page width?
*/
.swatch {

width: 100%;
height: 100%;
}
.swatch svg {
width: 100%;
height: 100%;
}

.category {
Expand All @@ -93,7 +102,7 @@ html.theme--documenter-dark .modal-card-head {
}

html.theme--documenter-dark p > code {
color: #eff !important;
color: #eff !important;
}


Expand All @@ -107,7 +116,7 @@ html.theme--documenter-dark a > code {
}

html.theme--documenter-dark p > a {
color: #eff !important;
color: #eff !important;
}

html.theme--documenter-dark .select select:focus,
Expand Down
95 changes: 71 additions & 24 deletions docs/src/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ When you start `using ColorSchemes`, it loads a set of pre-defined ColorSchemes

A ColorScheme is a Julia object which contains:

- an array of colors (eg `RGB(0.1, 0.3, 0.4)`)
- an ordered array of colors (see Colors.jl)
- a string defining a category
- a string that can contain descriptive notes
- a string containing descriptive notes

To access one of these built-in colorschemes, use its symbol:
To access one of the built-in colorschemes, use its symbol:

```@example
using Colors, ColorSchemes # hide
ColorSchemes.leonardo # or colorschemes[:leonardo]
ColorSchemes.leonardo
```

or

```@example
using Colors, ColorSchemes # hide
colorschemes[:leonardo]
```

The display depends on your working environment. If you’re using a notebook or IDE environment, the colors in the colorscheme should appear as a swatch in a cell or in a Plots window. Otherwise, you’ll see the colors listed as RGB values:
Expand Down Expand Up @@ -42,7 +49,7 @@ You can access the array of colors as:
ColorSchemes.leonardo.colors
```

By default, the colorscheme names aren’t imported. To avoid using the prefixes, you can import the ones that you want:
By default, the names of the built-in colorschemes aren’t imported. You can import the ones that you want:

```julia
julia> import ColorSchemes.leonardo
Expand All @@ -59,7 +66,7 @@ julia> leonardo
RGB{Float64}(0.972441,0.790701,0.285136)
```

You can reference a single value of a scheme:
You can reference a single color in a scheme:

```julia
leonardo[3]
Expand All @@ -79,10 +86,30 @@ leonardo[0.5]
-> RGB{Float64}(0.42637271063618504,0.28028983973265065,0.11258024276603132)
```

```@docs
get
With `get` you can resample an existing colorscheme, by passing a range:

```julia
get(ColorSchemes.darkrainbow, range(0.0, 1.0, length=14))

-> RGB{Float64}[
RGB{Float64}(0.237736, 0.340215, 0.575113),
RGB{Float64}(0.249978, 0.343813, 0.5620657),
RGB{Float64}(0.259452, 0.386963, 0.464862),
RGB{Float64}(0.272746, 0.439684, 0.3499807),
...
RGB{Float64}(0.72987,0.239399,0.230961),
RGB{Float64}(0.72987,0.239399,0.230961)]
```

and you can make a new scheme from this:

```@example
using ColorSchemes, Colors # hide
ndr = ColorScheme(get(ColorSchemes.darkrainbow, range(0.0, 1.0, length=14)))
```

See below for more possibilities.

## The colorschemes dictionary

The ColorSchemes module automatically provides a number of predefined schemes. All the colorschemes are stored in an exported dictionary, called `colorschemes`.
Expand Down Expand Up @@ -146,51 +173,69 @@ If you prefer, you can ‘roll your own’ search.

## Make your own colorscheme

Using Colors.jl, you can quickly define a range of colors and use it to make a colorscheme:
Using Colors.jl, you can use the `ColorScheme` constructor to make a range of colors into a colorscheme.

Using Colors.jl's `range()`:

```@example
using Colors, ColorSchemes
using Colors, ColorSchemes # hide
cs1 = ColorScheme(range(colorant"red", colorant"green", length=5))
```

Using `get()`:

```@example
using Colors, ColorSchemes
using Colors, ColorSchemes # hide
cs1 = ColorScheme(get(ColorSchemes.darkrainbow, range(0.3, 0.7, length=10)))
```

Converting from a sequential palette:

```@example
using Colors, ColorSchemes # hide
cs1 = ColorScheme(reverse(Colors.sequential_palette(300, 100, logscale=true)))
```

Or you can easily make your own by building an array:
From an array comprehension:

```@example
using Colors, ColorSchemes
using Colors, ColorSchemes # hide
mygrays = ColorScheme([RGB{Float64}(i, i, i) for i in 0:0.1:1.0])
```

A logarithmic scheme:

```@example
using Colors, ColorSchemes # hide
ColorScheme(get(ColorSchemes.darkrainbow, 10 .^ range(-2, stop=0, length=50)))
```

Give it a category or some added notes if you want:

```julia
mygrays = ColorScheme([RGB{Float64}(i, i, i) for i in 0:0.1:1.0],
"my useful schemes", "just some dull grey shades")
```

although this scheme won’t end up in the `colorschemes` dictionary.
although this scheme won’t be stored in the supplied `colorschemes` dictionary.

Another example, starting with a two-color scheme, then building a gradient from the first color to the other.
Another example: start with a two-color scheme, then building a gradient from the first color to the other.

```julia
myscheme = ColorScheme([Colors.RGB(1.0, 0.0, 0.0), Colors.RGB(0.0, 1.0, 0.0)],
"custom", "twotone, red and green")
ColorScheme([get(myscheme, i) for i in 0.0:0.01:1.0])
```

Another way is to use the [loadcolorscheme](@ref) function:
Another way is to use the [loadcolorscheme](@ref) function used to build the colorschemes dictionary when the package is loaded:

```julia
using Colors
using Colors, ColorSchemes
loadcolorscheme(:mygrays, [RGB{Float64}(i, i, i) for i in 0:0.1:1.0],
"useful schemes", "just some dull grey shades")
```

and that will be added (temporarily) to the built-in list.
and that will be added (temporarily) to the built-in dictionary.

```julia
julia> findcolorscheme("dull")
Expand All @@ -203,19 +248,21 @@ colorschemes containing "dull"
...found 1 result for "dull"
```

If you want to make more advanced ColorSchemes, use linear-segment dictionaries or indexed lists, and use functions to generate color values, see the `make_colorscheme()` function in the [ColorSchemeTools.jl](https://github.com/JuliaGraphics/ColorSchemeTools.jl) package.
!!! note

If you want to make more advanced ColorSchemes, use linear-segment dictionaries or indexed lists, and use functions to generate color values, see the `make_colorscheme()` function in the [ColorSchemeTools.jl](https://github.com/JuliaGraphics/ColorSchemeTools.jl) package.

## For CVD (color-vision deficient or "color-blind") users

This package contains a number of colorschemes that are designed to be helpful for people with some deficiencies in their perception of color:

- deuteranomaly (where green looks more red)
- *deuteranomaly* (where green looks more red)

- protanomaly (where red looks more green and less bright)
- *protanomaly* (where red looks more green and less bright)

- tritanomaly (difficult to tell the difference between blue and green, and between yellow and red)
- *tritanomaly* (difficult to tell the difference between blue and green, and between yellow and red)

- tritanopia (difficult to tell the difference between blue and green, purple and red, and yellow and pink)
- *tritanopia* (difficult to tell the difference between blue and green, purple and red, and yellow and pink)

```julia
findcolorscheme("cvd")
Expand Down Expand Up @@ -280,7 +327,7 @@ using Colors, ColorSchemes
ColorScheme(distinguishable_colors(10, transform=protanopic))
```

## Continuous color sampling
## More about color sampling

You can access the specific colors of a colorscheme by indexing (eg `leonardo[2]` or `leonardo[5:end]`). Or you can sample a ColorScheme at a point between 0.0 and 1.0 as if it were a continuous range of colors:

Expand Down
6 changes: 3 additions & 3 deletions docs/src/catalogue.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
```@setup catalog
# this code will display all the schemes in a category, as an SVG
# use in this docs/src/catalogue.md file as
# this code will display all the schemes in a category, as an SVG.
# Use in this docs/src/catalogue.md file as
# ```@example catalog
# using Luxor, ColorSchemes # hide
# ColorSchemeCategory("cmocean") # hide
Expand All @@ -20,9 +20,9 @@ function generate_scheme_svg(schemename;
cols = colorschemes[schemename].colors
l = length(cols)
Drawing(swatchwidth, swatchheight, :svg)
setline(0.5)
origin()
t = Tiler(swatchwidth, swatchheight, 1, l, margin=0)
setline(0.5)
for (i, c) in enumerate(cols)
sethue(c)
box(t, i, :fillstroke)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/functionindex.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Index
# Function index

```@index
```
6 changes: 6 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ colorschemes

findcolorscheme("purple")
# => display list of matching schemes

ColorScheme([colorant"red", colorant"green", colorant"blue"])
# new colorscheme from Colors.jl named colors

get(ColorSchemes.darkrainbow, range(0.0, 1.0, length=20)) |> ColorScheme
# new colorscheme by resampling existing
```

Original version by [cormullion](https://github.com/cormullion).
Expand Down
8 changes: 8 additions & 0 deletions src/ColorSchemes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ scheme = rand(keys(colorschemes))
"""
const colorschemes = Dict{Symbol,ColorScheme}()

"""
loadallschemes()
Load all colorschemes from data files. Not exported.
"""
function loadallschemes()
# load the installed schemes
datadir = joinpath(dirname(@__DIR__), "data")
Expand Down Expand Up @@ -280,6 +285,9 @@ img3 = get(colorschemes[:leonardo], 10.0 * rand(10, 10), (1.0, 9.0))
# Also works with PerceptualColourMaps
using PerceptualColourMaps # warning, installs PyPlot, PyCall, LaTeXStrings
img4 = get(PerceptualColourMaps.cmap("R1"), rand(10,10))
# resample an existing scheme to make a new one
get(ColorSchemes.darkrainbow, range(0.0, 1.0, length=13)) |> ColorScheme
```
"""
function get(cscheme::ColorScheme, x::AllowedInput, rangemode::Symbol)
Expand Down

0 comments on commit d801a9f

Please sign in to comment.