Skip to content

Commit

Permalink
Added rgb_sweep generator for sweeping through colors like spectrum_c…
Browse files Browse the repository at this point in the history
…olors
  • Loading branch information
LaurentRDC committed Jul 10, 2017
1 parent f7c84e9 commit be24e26
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion RELEASE.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Steps to Release scikit-ued
===========================

These are the steps to take to create a release of the module ``skued``:
These are the steps to take to create a release of the package ``skued``:

1. Switch to the release branch

Expand Down
2 changes: 1 addition & 1 deletion skued/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
translation_rotation_matrix)
from .array_utils import mirror, repeated_array
from .parallel import pmap, preduce
from .plot_utils import spectrum_colors
from .plot_utils import spectrum_colors, rgb_sweep
from .quantities import electron_wavelength, interaction_parameter, lorentz
from .voigt import gaussian, lorentzian, pseudo_voigt
54 changes: 53 additions & 1 deletion skued/plot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@
from collections import Iterable
from colorsys import hsv_to_rgb

def linspace(start, stop, num):
"""
Generate linear space
Parameters
----------
start : float
The starting value of the sequence.
stop : float
The end value of the sequence.
num : int, optional
Number of samples to generate.
Yields
------
val : float
"""
step = (stop - start)/num

val = start
for _ in range(num):
yield val
val += step

def spectrum_colors(num_colors):
"""
Generates a set of RGB colors corresponding to the visible spectrum (i.e. the rainbow).
Expand Down Expand Up @@ -35,4 +59,32 @@ def spectrum_colors(num_colors):

# Scale so that the maximum is 'purple' (hue = 0.8)
# otherwise plots don't look as good
yield from map(lambda hue: hsv_to_rgb(0.8*hue, s = 0.7, v = 0.9), hue_values)
yield from map(lambda hue: hsv_to_rgb(0.8*hue, s = 0.7, v = 0.9), hue_values)

def rgb_sweep(num_colors, source, dest):
"""
Generate a set of RGB colors as a linear sweep between two RGB colors `source` and `dest`.
These colors can be used by Matplotlib, PyQtGraph, Qt, and other plotting/graphics libraries.
Parameters
----------
num_colors : int
Number of colors to generate.
source : 3-tuple of floats
Source color in RGB space. Values should be between 0.0 and 1.0.
dest : 3-tuple of floats
Destination color in RGB space. Values should be between 0.0 and 1.0.
Yields
------
color : (R,G,B) tuple.
R, G, B values in a tuple, from 0.0 to 1.0.
"""
red_src, grn_src, blu_src = source
red_dst, grn_dst, blu_dst = dest

reds = linspace(red_src, red_dst, num = num_colors)
greens = linspace(grn_src, grn_dst, num = num_colors)
blues = linspace(blu_src, blu_dst, num = num_colors)

yield from zip(reds, greens, blues)
14 changes: 13 additions & 1 deletion skued/tests/test_plot_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import unittest
from .. import spectrum_colors
from .. import spectrum_colors, rgb_sweep

class TestSpectrumColors(unittest.TestCase):

Expand All @@ -23,6 +23,18 @@ def test_on_length_1_iterable(self):
""" Test that spectrum_colors is working on single-length iterables """
self.assertSequenceEqual(list(spectrum_colors(1)),
list(spectrum_colors([0])))

class TestRGBSweep(unittest.TestCase):

def test_on_ints(self):
""" Test the number of elements yielded from rgb_sweep """
colors = rgb_sweep(10, source = (1,0,0), dest = (0,1,0))
self.assertEqual(len(list(colors)), 10)

def test_source_equal_to_dest(self):
""" Test that rgb_sweep still works if source is equal to destination."""
colors = rgb_sweep(10, source = (1,0,0), dest = (1,0,0))
self.assertEqual(len(list(colors)), 10)

if __name__ == '__main__':
unittest.main()

0 comments on commit be24e26

Please sign in to comment.