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

Need colorPalette function to support mixed color configuration #16522

Closed
1 task done
wuzekang opened this issue May 10, 2019 · 2 comments
Closed
1 task done

Need colorPalette function to support mixed color configuration #16522

wuzekang opened this issue May 10, 2019 · 2 comments
Assignees
Labels

Comments

@wuzekang
Copy link
Contributor

wuzekang commented May 10, 2019

  • I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

Used to support configuration of dark background themes

BD0F07C1-E1CF-4c2a-90D2-AC43912B2D88

What does the proposed API look like?

@primary-1: color(~`colorPalette('@{primary-color}', '@{background-color}', 1) `); // replace tint(@primary-color, 90%)
@primary-2: color(~`colorPalette('@{primary-color}', '@{background-color}', 2) `); // replace tint(@primary-color, 80%)
@primary-3: color(~`colorPalette('@{primary-color}', '@{background-color}', 3) `); // unused
@wuzekang
Copy link
Contributor Author

A possible code implementation

/* stylelint-disable no-duplicate-selectors */
@import "bezierEasing";
@import "tinyColor";

// We create a very complex algorithm which take the place of original tint/shade color system
// to make sure no one can understand it 👻
// and create an entire color palette magicly by inputing just a single primary color.
// We are using bezier-curve easing function and some color manipulations like tint/shade/darken/spin
.colorPaletteMixin() {
@functions: ~`(function() {
  var hueStep = 2;
  var saturationStep = 16;
  var saturationStep2 = 5;
  var brightnessStep1 = 5;
  var brightnessStep2 = 15;
  var lightColorCount = 5;
  var darkColorCount = 4;

  var getHue = function(hsv, i, isLight) {
    var hue;
    if (hsv.h >= 60 && hsv.h <= 240) {
      hue = isLight ? hsv.h - hueStep * i : hsv.h + hueStep * i;
    } else {
      hue = isLight ? hsv.h + hueStep * i : hsv.h - hueStep * i;
    }
    if (hue < 0) {
      hue += 360;
    } else if (hue >= 360) {
      hue -= 360;
    }
    return Math.round(hue);
  };
  var getSaturation = function(hsv, i, isLight) {
    var saturation;
    if (isLight) {
      saturation = Math.round(hsv.s * 100) - saturationStep * i;
    } else if (i == darkColorCount) {
      saturation = Math.round(hsv.s * 100) + saturationStep;
    } else {
      saturation = Math.round(hsv.s * 100) + saturationStep2 * i;
    }
    if (saturation > 100) {
      saturation = 100;
    }
    if (isLight && i === lightColorCount && saturation > 10) {
      saturation = 10;
    }
    if (saturation < 6) {
      saturation = 6;
    }
    return Math.round(saturation);
  };
  var getValue = function(hsv, i, isLight) {
    if (isLight) {
      return Math.round(hsv.v * 100) + brightnessStep1 * i;
    }
    return Math.round(hsv.v * 100) - brightnessStep2 * i;
  };
  
  this.colorPalette = function(color, background, index) {
    var isInverse = tinycolor(background).toHsv().v < 0.5;
    var indexInverse = isInverse ? 11 - index : index;
    var isLight = indexInverse <= 6;
    var i = isLight ? lightColorCount + 1 - indexInverse : indexInverse - lightColorCount - 1;
    var hsv = tinycolor(color).toHsv();
    return tinycolor({
      h: getHue(hsv, i, isLight),
      s: getSaturation(hsv, i, isLight),
      v: getValue(hsv, i, isLight),
    }).toHexString();
  };
})()`;
}
// It is hacky way to make this function will be compiled preferentially by less
// resolve error: `ReferenceError: colorPalette is not defined`
// https://github.com/ant-design/ant-motion/issues/44
.colorPaletteMixin();

@zombieJ zombieJ assigned afc163 and unassigned yesmeck May 10, 2019
@zombieJ
Copy link
Member

zombieJ commented May 10, 2019

@afc163 pls help to check on this.

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

No branches or pull requests

5 participants