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

Avoid too similar colors #1

Open
Kikobeats opened this issue Dec 12, 2020 · 6 comments
Open

Avoid too similar colors #1

Kikobeats opened this issue Dec 12, 2020 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@Kikobeats
Copy link

Hey, I really love this library, thanks for that 🙂

Screen Shot 2020-12-12 at 18 07 52

I want to ask if could be possible to add a way to avoid too similar colors since they look too close and in some context that is confuse

@jessuni
Copy link
Owner

jessuni commented Jan 4, 2021

Sorry for the late reply. As I'm currently working on other projects, I could't spare much effort in this one. If you have found the solution, a PR is always welcome :)

If you're not in a hurry, I might dive into the codes and improve the algorithm a bit later.

@Kikobeats
Copy link
Author

Hey Jess, no rush!

I tried to improve the algorithm by myself but I don't understand the code too well 😅 so yea I can wait!

@jessuni jessuni self-assigned this Jan 6, 2021
@jessuni jessuni added the enhancement New feature or request label Jan 6, 2021
@pmartiner
Copy link

@Kikobeats what worked for me was the following:

  1. Adding a variable that stores the previous color so that the color param for the new SafeColor always contrasts with the previous one.
  2. Passed a unique string id so that when I generate a random color it's unique :)

@Kikobeats
Copy link
Author

@pmartiner that sounds smart!

Can you share the code snippet? 🙂

@pmartiner
Copy link

pmartiner commented Feb 16, 2021

Sure thing! My use case was rendering a Chart.js line chart with an automated color array with different and accessible colors, FYI.

        let prevColor: number[] = [255, 255, 255];

        const dataCampaignByDatesDataset = dataCampaignByDatesFiltered.map(obj => {
          const safeColor = new SafeColor({
            color: prevColor,
            contrast: 4.5,
          });
          const randomRgb = safeColor.random(obj.name);
          const rgbArray = randomRgb.substring(4, randomRgb.length - 1).split(',');
          const r = rgbArray[0];
          const g = rgbArray[1];
          const b = rgbArray[2];
          const alpha = 0.7;
          const rgb = `rgb(${r}, ${g}, ${b})`;
          const rgba = `rgb(${r}, ${g}, ${b}, ${alpha})`;
          prevColor = [r, g, b];

          return {
            label: obj.name,
            data: obj.amount,
            backgroundColor: rgba,
            borderColor: rgb,
            fill: 'start'
          };
        });

@jessuni
Copy link
Owner

jessuni commented Feb 18, 2021

@pmartiner wow, that's a smart workaround.

@Kikobeats what worked for me was the following:

  1. Adding a variable that stores the previous color so that the color param for the new SafeColor always contrasts with the previous one.
  2. Passed a unique string id so that when I generate a random color it's unique :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants