Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make individual colors from Vega color schemes accessible from Altair #2617

Open
1 task
Tracked by #2861
joelostblom opened this issue Jun 15, 2022 · 2 comments
Open
1 task
Tracked by #2861
Labels
enhancement vega: vega Requires upstream action in `vega`

Comments

@joelostblom
Copy link
Contributor

joelostblom commented Jun 15, 2022

Tasks

Description

I think it would be valuable to make all the individual colors from Vega schemes accessible programmatically from Altair. Since Vega stores them in a single file, we could parse that and create a Python dictionary that could be used to access single colors from each color scheme. Something like this:

Code block

pd.read_table(
    'https://raw.githubusercontent.com/vega/vega/v5.21.0/packages/vega-scale/src/palettes.js',
    skipinitialspace=True,
    sep=':',
).drop(
    index=['export const discrete = {', '};']
).iloc[:, 0].str.replace(
    "'", ""
).str.replace(
    ',', ''
).apply(
    lambda x: ["#" + x[i:i+6] for i in range(0, len(x), 6)]
).to_dict()
{'blues': [
  '#cfe1f2',
  '#bed8ec',
  '#a8cee5',
  '#8fc1de',
  '#74b2d7',
  '#5ba3cf',
  '#4592c6',
  '#3181bd',
  '#206fb2',
  '#125ca4',
  '#0a4a90'],
 'greens': [
  '#d3eecd',
  '#c0e6ba',
  '#abdda5',
  '#94d391',
  '#7bc77d',
  '#60ba6c',
  '#46ab5e',
  '#329a51',
  '#208943',
  '#0e7735',
  '#036429']
...

@dangotbanned
Copy link
Member

@joelostblom I'm adding some context for what is going on in your script, and linking docs for Vega/Color Schemes

The lambda appears to be similar to d3-scale-chromatic/src/colors.js, with an example of usage in category10.js.
In short, splitting a string containing multiple hexadecimal colors

src/categorical/category10.js

https://github.com/d3/d3-scale-chromatic/blob/2c52792197299346b7bdb94322bb4dff8f554fea/src/categorical/category10.js#L3

A related comment by @domoritz (vega/vega#3843 (comment))

import colors from "../colors.js";

export default colors("1f77b4ff7f0e2ca02cd627289467bd8c564be377c27f7f7fbcbd2217becf");

The current palettes.js has changed since this issue was opened.
Lines L1-L12 and L78-L94 would probably break the initial parser idea.

I do agree these would be nice to have available in altair.
I think we should try to push for a Public API for this upstream in (https://github.com/vega/vega) - since that would be safer than dependending on internal library code.
That route was really help for me when I started reaching for vl_convert internals in vega/vl-convert#191

@dangotbanned dangotbanned added the vega: vega Requires upstream action in `vega` label Jan 7, 2025
@joelostblom
Copy link
Contributor Author

I think we should try to push for a Public API for this upstream in (https://github.com/vega/vega) - since that would be safer than dependending on internal library code.

Yes, I agree that this is the better option

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement vega: vega Requires upstream action in `vega`
Projects
None yet
Development

No branches or pull requests

2 participants