Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Last active November 26, 2018 07:30
Show Gist options
  • Save CaiJingLong/60b9761a6bc5107caf0e4bf466ebfce8 to your computer and use it in GitHub Desktop.
Save CaiJingLong/60b9761a6bc5107caf0e4bf466ebfce8 to your computer and use it in GitHub Desktop.
ant design v3 颜色转换函数
Color calcAntdColor(Color color, int index) {
var helper = _AntdColorHelper.instance;
var isLight = index <= 6;
var hsv = HSVColor.fromColor(color);
int i = isLight
? _AntdColorHelper.lightColorCount + 1 - index
: index - _AntdColorHelper.lightColorCount - 1;
return HSVColor.fromAHSV(
color.opacity,
helper.getHue(hsv, i, isLight),
helper.getSaturation(hsv, i, isLight),
helper.getValue(hsv, i, isLight).roundToDouble())
.toColor();
}
class _AntdColorHelper {
static const hueStep = 2;
static const saturationStep = 16;
static const saturationStep2 = 5;
static const brightnessStep1 = 5;
static const brightnessStep2 = 15;
static const lightColorCount = 5;
static const darkColorCount = 4;
const _AntdColorHelper();
static _AntdColorHelper instance = _AntdColorHelper();
double getHue(HSVColor hsv, int i, bool isLight) {
double hue;
if (hsv.hue >= 60 && hsv.hue <= 240) {
hue = isLight ? hsv.hue - hueStep * i : hsv.hue + hueStep * i;
} else {
hue = isLight ? hsv.hue + hueStep * i : hsv.hue - hueStep * i;
}
if (hue < 0) {
hue += 360;
} else if (hue >= 360) {
hue -= 360;
}
return hue;
}
double getSaturation(HSVColor hsv, int i, bool isLight) {
double saturation;
if (isLight) {
saturation = (hsv.saturation * 100) - saturationStep * i;
} else if (i == darkColorCount) {
saturation = (hsv.saturation * 100) + saturationStep;
} else {
saturation = (hsv.saturation * 100) + saturationStep2 * i;
}
if (saturation > 100) {
saturation = 100;
}
if (isLight && i == lightColorCount && saturation > 10) {
saturation = 10;
}
if (saturation < 6) {
saturation = 6;
}
return saturation;
}
int getValue(HSVColor hsv, int i, bool isLight) {
if (isLight) {
return (hsv.value * 100).round() + brightnessStep1 * i;
}
return (hsv.value * 100).round() - brightnessStep2 * i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment