• Stars
    star
    257
  • Rank 157,788 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy color scales and color conversion for Python.

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 colormath and grapefruit. Spectra is enormously inspired by chroma.js and d3's scales.

Installation

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.

teal_lab = spectra.html("teal").to("lab")
print(teal_lab.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.

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.)

teal = spectra.html("teal")
light_teal = light_teal.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:

my_scale = spectra.scale([ "gray", "red" ])
halfway = my_scale(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.:

my_scale = spectra.scale([ "gray", "red" ]).domain([ 10, 20 ])
halfway = my_scale(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:

my_scale = spectra.scale([ "gray", "red" ])
my_range = my_scale.range(5)

print(my_range)
>>> [<spectra.core.Color object at 0x10f4759d0>, <spectra.core.Color object at 0x10f475a90>, 
    <spectra.core.Color object at 0x10f475b50>, <spectra.core.Color object at 0x10f475cd0>, 
    <spectra.core.Color object at 0x10f475d50>]

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.

More Repositories

1

pdfplumber

Plumb a PDF for detailed information about each char, rectangle, line, et cetera โ€”ย and easily extract text and tables.
Python
6,285
star
2

markovify

A simple, extensible Markov chain generator.
Python
3,292
star
3

waybackpack

Download the entire Wayback Machine archive for a given URL.
Python
2,841
star
4

nbpreview

Render Jupyter/IPython notebooks without running a notebook server.
CSS
289
star
5

notebookjs

Render Jupyter/IPython notebooks on the fly, in the browser. (Or on the command line, if you'd like.)
JavaScript
272
star
6

envplus

Combine your Python virtualenvs.
Python
115
star
7

weightedcalcs

Pandas-based utility to calculate weighted means, medians, distributions, standard deviations, and more.
Python
103
star
8

reporter

Literate data analysis with iPython notebooks and Jekyll.
Ruby
92
star
9

twick

Twitter, quick. Fetch and store tweets on short notice.
Python
80
star
10

intro-to-visidata

Source files for "An Introduction to VisiData"
HTML
70
star
11

visidata-plugins

A place for me to share VisiData plugins I've written.
Python
36
star
12

mplstyle

A simple API for setting matplotlib styles, as well as a repository of nice styles.
Python
32
star
13

visidata-cheat-sheet

A one-page cheat sheet for VisiData, available in multiple languages.
HTML
26
star
14

gekyll

A Jekyll plugin for using Git repositories as posts, giving you access to a post's commits, diffs, and more.
Ruby
25
star
15

nbexec

A dead-simple tool for executing Jupyter notebooks from the command line.
Python
20
star
16

Backbone.Table

Render any Backbone.js Collection as an HTML table.
JavaScript
20
star
17

buzzfeed-news-trending-strip

Dataset: BuzzFeed News โ€œTrendingโ€ Strip, 2018โ€“2023
Python
19
star
18

tab-bankrupter

A Chrome extension for declaring "tab bankruptcy" without losing all your links.
JavaScript
18
star
19

astronomer

Fetch information about the users who've starred a given GitHub repository.
Python
17
star
20

txtbirds

โ€พโ€พ\/โ€พโ€พ
JavaScript
14
star
21

tinyapi

Python wrapper around TinyLetter's publicly accessible โ€” but undocumented โ€” API.
Python
13
star
22

fbpagefeed

A library and command-line tool for fetching Facebook Pages' published posts.
Python
12
star
23

virtualenv-recipes

Recipes for useful Python virtualenvs.
Shell
12
star
24

data-tactics

Half-baked idea: Conceptual building blocks for data analysis.
11
star
25

tinystats

Command-line tool for fetching message, URL, and subscriber data for the TinyLetter newsletters you own.
Python
11
star
26

vinejs

Somewhere between a total joke and a useful library for fetching Vine.co videos.
JavaScript
11
star
27

nicar-2024-pdfplumber-workshop

Jupyter Notebook
11
star
28

mta-colors

CSS & JSON files to help developers use the official colors of New York's Metropolitan Transportation Authority.
CSS
10
star
29

compleat

Fetch autocomplete suggestions from Google Search.
Python
9
star
30

google-table-converter

A browser-based tool for converting Google Spreadsheets into responsive HTML <table>s.
HTML
9
star
31

lede-2023

Jupyter Notebook
8
star
32

gifparse

[Work in progress.] Parse the GIF 89a file format, down to the minor details. Pure Python, no dependencies.
Python
8
star
33

nicar-2015-schedule

NICAR 2015 conference schedule as CSV and JSON, plus the underlying Python scraper.
Python
8
star
34

WRIT1-CE9741

WRIT1-CE9741, Fall 2013, NYU School of Continuing and Professional Studies
Ruby
6
star
35

nicar-2023-pdfplumber-workshop

Jupyter Notebook
6
star
36

csvcat

Efficiently concatenate CSVs (or other tabular text files), stripping extra header lines.
Shell
6
star
37

nicar-2017-schedule

NICAR 2017 conference schedule as JSON and CSV, plus the underlying Python scraper.
Python
6
star
38

babynames

CSVs and parsers for the Social Security Administration's historical baby name data.
Python
5
star
39

minicard

A bare-bones CSS stylesheet for creating "card"-style elements.
CSS
4
star
40

macmailer

Command-line utility and Ruby library for creating/sending messages in OSX's Mail.app program.
Ruby
4
star
41

nicar-now

Your unofficial guide to what's happening next at NICAR 2020.
3
star
42

text-toggle

Let readers toggle between two versions of a text.
JavaScript
3
star
43

fidget

Fidget.js is a small, configurable JavaScript library that resizes blocks of text to fit their containers.
JavaScript
3
star
44

statusfiles

IDEA: A simple, structured, standardized, technology-agnostic way to represent the status of things.
3
star
45

nicar-2018-schedule

Your unofficial guide to what's happening next at NICAR 2018.
Python
3
star
46

glat-glong

Find the precise latitude and longitude of any point on Google Maps. A Chrome extension.
JavaScript
3
star
47

lede-2024

Jupyter Notebook
3
star
48

gmap-button

A JavaScript library for adding buttons to embedded Google Maps.
JavaScript
2
star
49

crochet

Hook into and/or monkeypatch any Ruby class- or instance-method. Provides 'before' and 'after' hooks, plus their destructive evil twins.
Ruby
2
star
50

jub

As in, "get the jub done." Or as in, "jQuery, Underscore, Backbone." It's a shell script that automatically grabs the latest versions of those libraries, so that you can get on with prototyping.
Shell
2
star
51

download-all-attachments-from-a-gmail-conversation

Two methods that *seem* to work...
1
star
52

fbiter

A simple library for iterating through paginated Facebook API endpoints.
Python
1
star
53

weddingroulette

The code behind http://weddingroulette.com/
Ruby
1
star
54

jekyll-auto-s3

Automatically sync your Jekyll project to S3 on every (re)build.
Ruby
1
star
55

griddle

Griddle.js is lightweight tool for creating and manipulating programmable, fluid, shift-able grids.
JavaScript
1
star
56

linstapaper

Article-list and site files for linstapaper.com
JavaScript
1
star
57

nbtemplate

Render iPython notebooks to other layouts, via templates. Library and command-line tool.
Python
1
star
58

nicar-2019-schedule

The NICAR 2019 conference schedule as JSON and CSV files, plus the underlying Python scraper.
Python
1
star
59

parabear

An experiment in stupid-simple HTML article text extraction.
JavaScript
1
star