spectra

Easy color scales and color conversion for Python.

https://github.com/jsvine/spectra

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (6.8%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Easy color scales and color conversion for Python.

Basic Info
Statistics
  • Stars: 261
  • Watchers: 8
  • Forks: 9
  • Open Issues: 7
  • Releases: 1
Created over 11 years ago · Last pushed over 1 year ago
Metadata Files
Readme License

README.md

Spectra

Spectra is a Python library that makes color math, color scales, and color-space conversion easy. Support for:

  • Color scales
  • Color ranges
  • Color blending
  • Brightening/darkening colors
  • Saturating/desaturating colors
  • Conversion to/from multiple color spaces

Spectra is built on colormath2 and grapefruit. Spectra is enormously inspired by chroma.js and d3's scales.

Installation

sh pip install spectra

Walkthrough

See this walkthrough to see what Spectra can do.

API

Creating color objects from web-colors or hexcode strings

spectra.html(html_string)

E.g., spectra.html("papayawhip"), spectra.html("#BAABAA") spectra.html("#FFF")

Creating color objects from color space values

spectra.rgb(r, g, b)

Specifically: sRGB


spectra.lab(L, a, b)

Specifically: CIELAB


spectra.lch(L, c, h)

Also known elsewhere as "hcl"


spectra.hsl(h, s, l)

spectra.hsv(h, s, v)

spectra.xyz(x, y, z)

spectra.cmy(c, m, y)

spectra.cmky(c, m, y, k)

Getting color values

Instances of spectra.Color have four main properties:

  • .values: An array representation of the color's values in its own color space, e.g. (L, a, b) for an lab color.
  • .hexcode: The hex encoding of this color, e.g. #ffffff for rgb(255, 255, 255)/html(\"white\").
  • .rgb: The (r, g, b) values for this color in the rgb color space; these are allowed to go out of gamut.
  • .clamped_rgb: The \"clamped\" (r, g, b) values for this color in the rgb color space.

Note on .rgb and .rgb_clamped: Spectra follows colormath's convention:

RGB spaces tend to have a smaller gamut than some of the CIE color spaces. When converting to RGB, this can cause some of the coordinates to end up being out of the acceptable range (0.0-1.0 or 1-255, depending on whether your RGB color is upscaled). [...] Rather than clamp these for you, we leave them as-is.

Modifying colors

color.to(space)

Convert this color to another color space.

```python teallab = spectra.html("teal").to("lab") print(teallab.values)

(48.25453959565715, -28.843707890081394, -8.48135382506432) ```


color.blend(other_color, ratio=0.5)

Blend this color with another color, using ratio of that other color.

```python yellow, red = spectra.html("red"), spectra.html("yellow") orange = yellow.blend(red) print(orange.hexcode)

'#ff8000' ```


color.brighten(amount=10)

Brighten this color by amount luminance. (Converts this color to the LCH color space, and then increases the L parameter by amount.)

```python teal = spectra.html("teal") lightteal = lightteal.brighten(30) print(light_teal.hexcode)

'#75d1d0' ```


color.darken(amount=10)

The opposite of color.brighten; reduces color by amount luminance.


color.saturate(amount=10)

Saturate this color by amount chroma. (Converts this color to the LCH color space, and then increases the C parameter by amount.)


color.desaturate(amount=10)

The opposite of color.saturate; reduces color by amount chroma.


Creating color scales

spectra.scale(colors)

colors should be a list of two or more colors (created by any of the methods above), web-color names, or hexcodes.

Returns a spectra.Scale object, which translates numbers to their corresponding colors:

```python myscale = spectra.scale([ "gray", "red" ]) halfway = myscale(0.5) print(halfway.hexcode)

'#c04040' ```


Modifying color scales

scale.domain(numbers)

By default, a scale's domain is [ 0, 1 ]. But you can change it to be anything else, e.g.:

```python myscale = spectra.scale([ "gray", "red" ]).domain([ 10, 20 ]) halfway = myscale(15) print(halfway.hexcode)

'#c04040' ```


Creating color ranges

scale.range(count)

This function returns a list of spectra.Color objects evenly spaced between a scale's colors. For example:

```python myscale = spectra.scale([ "gray", "red" ]) myrange = my_scale.range(5)

print(my_range)

[, , , , ]

print([ c.hexcode for c in my_range ])

['#808080', '#a06060', '#c04040', '#df2020', '#ff0000'] ```

Alternatively, as a shortcut, you can use spectra.range(colors, count).


Feedback/Suggestions

Issues and pull requests very much appreciated.

Owner

  • Name: Jeremy Singer-Vine
  • Login: jsvine
  • Kind: user

Liberating data @ data-liberation-project.org, newslettering @ data-is-plural.com

GitHub Events

Total
  • Create event: 1
  • Release event: 1
  • Issues event: 6
  • Watch event: 6
  • Issue comment event: 11
  • Push event: 3
  • Pull request event: 6
  • Fork event: 2
Last Year
  • Create event: 1
  • Release event: 1
  • Issues event: 6
  • Watch event: 6
  • Issue comment event: 11
  • Push event: 3
  • Pull request event: 6
  • Fork event: 2

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 33
  • Total Committers: 4
  • Avg Commits per committer: 8.25
  • Development Distribution Score (DDS): 0.121
Past Year
  • Commits: 4
  • Committers: 2
  • Avg Commits per committer: 2.0
  • Development Distribution Score (DDS): 0.5
Top Committers
Name Email Commits
Jeremy Singer-Vine j****e@g****m 29
Robert Smallshire r****t@s****k 2
Tim Gates t****s@i****m 1
Phil Ewels p****s@s****e 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 19
  • Total pull requests: 9
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 6 days
  • Total issue authors: 16
  • Total pull request authors: 5
  • Average comments per issue: 2.26
  • Average comments per pull request: 0.89
  • Merged pull requests: 7
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 3
  • Pull requests: 6
  • Average time to close issues: 10 days
  • Average time to close pull requests: 6 days
  • Issue authors: 2
  • Pull request authors: 2
  • Average comments per issue: 2.33
  • Average comments per pull request: 1.0
  • Merged pull requests: 4
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • rob-smallshire (2)
  • jsvine (2)
  • gfairchild (2)
  • cameron-finn (1)
  • pojoba02 (1)
  • PalmDr (1)
  • earthbound19 (1)
  • araichev (1)
  • emlyn (1)
  • abingham (1)
  • briantjacobs (1)
  • mazznoer (1)
  • AI-ML-Enthusiast (1)
  • connorferster (1)
  • tony (1)
Pull Request Authors
  • rob-smallshire (4)
  • apraga (2)
  • timgates42 (1)
  • ewels (1)
  • jsvine (1)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 28,422 last-month
  • Total docker downloads: 87,084
  • Total dependent packages: 6
    (may contain duplicates)
  • Total dependent repositories: 92
    (may contain duplicates)
  • Total versions: 16
  • Total maintainers: 1
pypi.org: spectra

Color scales and color conversion made easy for Python.

  • Versions: 13
  • Dependent Packages: 5
  • Dependent Repositories: 77
  • Downloads: 28,422 Last month
  • Docker Downloads: 87,084
Rankings
Docker downloads count: 0.8%
Dependent repos count: 1.7%
Dependent packages count: 1.8%
Downloads: 2.7%
Average: 3.5%
Stargazers count: 4.0%
Forks count: 10.2%
Maintainers (1)
Last synced: 11 months ago
conda-forge.org: spectra

Spectra is a Python library that makes color math, color scales, and color-space conversion easy. It has support for Color scales, Color ranges, Color blending, Brightening/darkening colors, Saturating/desaturating colors and Conversion to/from multiple color spaces. Spectra is built on colormath and grapefruit. Spectra is enormously inspired by chroma.js and d3's scales.

  • Versions: 3
  • Dependent Packages: 1
  • Dependent Repositories: 15
Rankings
Dependent repos count: 9.2%
Stargazers count: 23.9%
Average: 26.8%
Dependent packages count: 29.0%
Forks count: 45.0%
Last synced: 11 months ago

Dependencies

requirements.txt pypi
  • colormath >=3.0.0
setup.py pypi