gpt4 book ai didi

java - 将 colorPrimary 转换为 colorPrimaryDark(暗多少)

转载 作者:IT老高 更新时间:2023-10-28 23:32:04 27 4
gpt4 key购买 nike

在 Material Design 的指导下,状态栏应该比操作栏暗多少?我在运行时为操作栏设置了颜色,并且在编程时无法知道该颜色,那么如何获得正确的状态栏颜色?

我知道我可以用这个来加深颜色

this.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(colorPrimary));                              
float[] hsv = new float[3];
Color.colorToHSV(colorPrimary, hsv);
hsv[2] *= 0.8f;
int colorPrimaryDark = Color.HSVToColor(hsv);

if(Build.VERSION.SDK_INT>=21)
Chat.this.getWindow().setStatusBarColor(colorPrimaryDark);

但我不确定要变暗多少

最佳答案

Material 设计调色板不是通过在 HSV 中操作颜色生成的。它是使用 HSL(色相、饱和度、亮度)完成的。

这是一个使用 HSL 使颜色变暗/变亮的实用程序类

package com.ammar.materialcolorizer;

import android.graphics.Color;

/**
* A utility class for darkening and lightening colors in the same way as
* material design color palettes
* Created by Ammar Mardawi on 12/4/16.
*/

public class ColorUtil {

/**
* Darkens a given color
* @param base base color
* @param amount amount between 0 and 100
* @return darken color
*/
public static int darken(int base, int amount) {
float[] hsv = new float[3];
Color.colorToHSV(base, hsv);
float[] hsl = hsv2hsl(hsv);
hsl[2] -= amount / 100f;
if (hsl[2] < 0)
hsl[2] = 0f;
hsv = hsl2hsv(hsl);
return Color.HSVToColor(hsv);
}

/**
* lightens a given color
* @param base base color
* @param amount amount between 0 and 100
* @return lightened
*/
public static int lighten(int base, int amount) {
float[] hsv = new float[3];
Color.colorToHSV(base, hsv);
float[] hsl = hsv2hsl(hsv);
hsl[2] += amount / 100f;
if (hsl[2] > 1)
hsl[2] = 1f;
hsv = hsl2hsv(hsl);
return Color.HSVToColor(hsv);
}


/**
* Converts HSV (Hue, Saturation, Value) color to HSL (Hue, Saturation, Lightness)
* Credit goes to xpansive
* https://gist.github.com/xpansive/1337890
* @param hsv HSV color array
* @return hsl
*/
private static float[] hsv2hsl(float[] hsv) {
float hue = hsv[0];
float sat = hsv[1];
float val = hsv[2];

//Saturation is very different between the two color spaces
//If (2-sat)*val < 1 set it to sat*val/((2-sat)*val)
//Otherwise sat*val/(2-(2-sat)*val)
//Conditional is not operating with hue, it is reassigned!
// sat*val/((hue=(2-sat)*val)<1?hue:2-hue)
float nhue = (2f - sat) * val;
float nsat = sat * val / (nhue < 1f ? nhue : 2f - nhue);
if (nsat > 1f)
nsat = 1f;

return new float[]{
//[hue, saturation, lightness]
//Range should be between 0 - 1
hue, //Hue stays the same

// check nhue and nsat logic
nsat,

nhue / 2f //Lightness is (2-sat)*val/2
//See reassignment of hue above
};
}

/**
* Reverses hsv2hsl
* Credit goes to xpansive
* https://gist.github.com/xpansive/1337890
* @param hsl HSL color array
* @return hsv color array
*/
private static float[] hsl2hsv(float[] hsl) {
float hue = hsl[0];
float sat = hsl[1];
float light = hsl[2];

sat *= light < .5 ? light : 1 - light;

return new float[]{
//[hue, saturation, value]
//Range should be between 0 - 1

hue, //Hue stays the same
2f * sat / (light + sat), //Saturation
light + sat //Value
};
}
}

根据Material Design Color Generator ,要生成primaryColorDark,您需要变暗12。以下是如何生成与Material Design Color Generator 完全相同的完整调色板:

    setColor("50", ColorUtil.lighten(color, 52), mTv50);
setColor("100", ColorUtil.lighten(color, 37), mTv100);
setColor("200", ColorUtil.lighten(color, 26), mTv200);
setColor("300", ColorUtil.lighten(color, 12), mTv300);
setColor("400", ColorUtil.lighten(color, 6), mTv400);

setColor("500", ColorUtil.lighten(color, 0), mTv500);

setColor("600", ColorUtil.darken(color, 6), mTv600);
setColor("700", ColorUtil.darken(color, 12), mTv700);
setColor("800", ColorUtil.darken(color, 18), mTv800);
setColor("900", ColorUtil.darken(color, 24), mTv900);

关于java - 将 colorPrimary 转换为 colorPrimaryDark(暗多少),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30870167/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com