Skip to content

Commit

Permalink
Add get_shades()
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Jan 23, 2025
1 parent b2dc269 commit cc0e4b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/jcvi/graphics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

mpl.use("Agg")

import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

Expand Down Expand Up @@ -677,6 +678,31 @@ def get_intensity(octal):
return intensity


def get_shades(base_color: str, n: int) -> np.array:
"""
Generate N shades of a base color by blending with white and black. The base
color is in hex format, e.g., '#FF0000' for red.
"""
base_rgb = np.array(mcolors.to_rgb(base_color)) # Convert hex to RGB (normalized)
lighter = n // 2
darker = n - lighter - 1

# Generate lighter shades by blending with white
white = np.array([1, 1, 1]) # White color as a NumPy array
lighter_shades = [
(white * (1 - i) + base_rgb * i) for i in np.linspace(0.3, 1, lighter + 1)
][:-1]

# Generate darker shades by blending with black
black = np.array([0, 0, 0]) # Black color as a NumPy array
darker_shades = [
(base_rgb * i + black * (1 - i)) for i in np.linspace(1, 0.2, darker + 1)
][1:]

# Combine all shades with the base color in the middle
return lighter_shades + [base_rgb] + darker_shades


def adjust_spines(ax, spines, outward=False, color="lightslategray"):
# Modified from <http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html>
for loc, spine in ax.spines.items():
Expand Down
2 changes: 1 addition & 1 deletion src/jcvi/projects/sugarcane.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def simulate_BCn(n: int, SO: Genome, SS: Genome, mode: CrossMode, verbose=False)
][n]


def plot_summary(ax, samples: list[Genome]) -> GenomeSummary:
def plot_summary(ax, samples: List[Genome]) -> GenomeSummary:
"""Plot the distribution of chromosome numbers given simulated samples.
Args:
Expand Down

0 comments on commit cc0e4b7

Please sign in to comment.