Java 将colorPrimary转换为colorPrimaryDark(颜色深多少)
在材料设计指导中,状态栏应该比动作栏暗多少? 我在运行时为actionbar设置了颜色,但在编程时无法知道此颜色,因此如何获得正确的状态栏颜色 我知道我可以用这个把颜色调暗Java 将colorPrimary转换为colorPrimaryDark(颜色深多少),java,android,material-design,Java,Android,Material Design,在材料设计指导中,状态栏应该比动作栏暗多少? 我在运行时为actionbar设置了颜色,但在编程时无法知道此颜色,因此如何获得正确的状态栏颜色 我知道我可以用这个把颜色调暗 this.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(colorPrimary)); float[] hsv = new float[3]; Color.colorToHSV(color
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);
但我不知道要把它弄暗多少
谷歌建议在你的应用程序中使用500种颜色作为原色,其他颜色作为强调色
工具栏和较大的色块应使用应用程序原色的500色
所以原色应该是色调500
状态栏应为原色的较深700色调
所以primaryColorDark应该是色调700
所以我想primaryColorDark应该比primaryColor深200色调。
嗯,它应该有多“暗”,这取决于你自己
如果您使用的是Android Studio,在colors.xml中,双击颜色预览,切换到HSV模式,然后降低亮度。(与您以编程方式执行的操作类似)材质设计调色板不是通过在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
};
}
}
一种确切知道它应该暗多少的方法是使用。只需输入原色的十六进制值,它将为您生成明暗版本。Android java代码:
public static int darkenColor(int color) {
int alpha = Color.alpha(color);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, (int) (red * 0.9), (int) (green * 0.9), (int) (blue * 0.9));
}
public static String intColor2String(int intColor) {
String strColor = String.format("#%06X", 0xFFFFFF & intColor);
return strColor;
}
如何使用
String dakenColor = Utils.intColor2String(
Utils.darkenColor(Color.parseColor("#21BBA6")));
该官员基于JS库,使用chroma.darken()/chroma.brighlight()函数计算变暗/变亮的颜色,该函数的算法基于
Github上的源代码片段
材质颜色工具仅使用amount参数的回退值,当参数为18时,它将减去18(变暗)/增加18(变亮)到给定基色的亮度分量L*。右侧。但是如果我知道500的颜色代码,我如何通过编程将其转换为700?如果你有你的tint 500颜色,你可以使用RGB这样的颜色:R*0.7,G*0.7,B*0.7来获得tint 700颜色,我相信。500和700是调色板,但他已经选择了颜色,可能不是500。你没有严格遵守从第一次开始的规则指导方针不是规则。OP:选择一种颜色,使用*0.5和*0.7的RB比率获得一个好的、MD友好的渐变。剩下的就看你了。用RGB值乘以0.7并不是一种使颜色变暗的一致方法,而且他们也没有用这种方法从500种颜色中得到700种颜色。根据您变暗的颜色,某些颜色会比其他颜色更暗。实现这一点的更好方法是将rgb转换为HSL颜色空间,然后根据您希望亮度值变暗的程度从亮度值中减去,然后再转换回rgb。我会将亮度值降低多少取决于您。。。你更喜欢什么,是什么让你和你的初选很好的结合。也许你不应该严格遵循指南。在这种情况下,建议不能适用于所有颜色。好的方面,指南不是规则。谢天谢地,你给了他正确的答案。我承认我的答案是“意见库”,我一直在寻找一种编程的方法,因为我是从一个可能是任何东西的图像生成主图像的。不过还是要谢谢你,这看起来是一个很好的资源。这里是到材质颜色工具的直接链接:。在页面上,单击自定义链接(位于右上角附近)并输入十六进制颜色,以显示该颜色的暗色和浅色变化(以及十六进制值)。欢迎使用SO!当你发布一个答案时,尽量解释一下。这个代码是如何工作的?该产品有哪些优点:?
String dakenColor = Utils.intColor2String(
Utils.darkenColor(Color.parseColor("#21BBA6")));
Color.prototype.darken = function(amount=1) {
const me = this;
const lab = me.lab();
lab[0] -= LAB_CONSTANTS.Kn * amount;
return new Color(lab, 'lab').alpha(me.alpha(), true);
}