如何使用字符串(fomat十六进制)初始化@angular material components/color picker side.ts
我试着 将十六进制字符串转换为@Angle material components/颜色选择器的颜色导入 或者实例化颜色并在数据库中设置我的值 从侧面看 mat表单字段>如何使用字符串(fomat十六进制)初始化@angular material components/color picker side.ts,angular,color-picker,angular10,Angular,Color Picker,Angular10,我试着 将十六进制字符串转换为@Angle material components/颜色选择器的颜色导入 或者实例化颜色并在数据库中设置我的值 从侧面看 mat表单字段> 首先,您需要将颜色转换为RGB。您可以使用以下方法: hexToRgb(hex) { const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; hex = hex.replace(shorthandRegex, (m, r, g, b) => {
首先,您需要将颜色转换为RGB。您可以使用以下方法:
hexToRgb(hex) {
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, (m, r, g, b) => {
return r + r + g + g + b + b;
});
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
然后,您需要获得NgxMatColorPickerInput的一个实例
import { NgxMatColorPickerInput, Color } from '@angular-material-components/color-picker';
现在您可以像这样轻松地设置值
ngAfterViewInit(): void {
const temp = this.hexToRgb('#00ff00');
this.pickerInput.value = new Color(temp.r, temp.g, temp.b);
}
首先,您需要将颜色转换为RGB。您可以使用以下方法:
hexToRgb(hex) {
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, (m, r, g, b) => {
return r + r + g + g + b + b;
});
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
然后,您需要获得NgxMatColorPickerInput的一个实例
import { NgxMatColorPickerInput, Color } from '@angular-material-components/color-picker';
现在您可以像这样轻松地设置值
ngAfterViewInit(): void {
const temp = this.hexToRgb('#00ff00');
this.pickerInput.value = new Color(temp.r, temp.g, temp.b);
}
好的,谢谢
createPriorityp:FormGroup{
设rgb=this.hexToRgbp.color
返回此.formBuilder.group{
名称:[p.name,Validators.compose[Validators.required,Validators.maxLength25]],
颜色:[新颜色rgb.r,rgb.g,rgb.b,1,Validators.compose[Validators.required]]
};
}
hexToRgbhex{
const shorthandRegex=/^?[a-f\d][a-f\d][a-f\d]$/i;
hex=hex.replaceshorthandRegex,m,r,g,b=>{
返回r+r+g+g+b+b;
};
const result=/^?[a-f\d]{2}[a-f\d]{2}[a-f\d]{2}$/i.exechex;
返回结果{
r:parseIntresult[1],16,
g:parseIntresult[2],16,
b:parseIntresult[3],16
}:null;
} 好的,谢谢
createPriorityp:FormGroup{
设rgb=this.hexToRgbp.color
返回此.formBuilder.group{
名称:[p.name,Validators.compose[Validators.required,Validators.maxLength25]],
颜色:[新颜色rgb.r,rgb.g,rgb.b,1,Validators.compose[Validators.required]]
};
}
hexToRgbhex{
const shorthandRegex=/^?[a-f\d][a-f\d][a-f\d]$/i;
hex=hex.replaceshorthandRegex,m,r,g,b=>{
返回r+r+g+g+b+b;
};
const result=/^?[a-f\d]{2}[a-f\d]{2}[a-f\d]{2}$/i.exechex;
返回结果{
r:parseIntresult[1],16,
g:parseIntresult[2],16,
b:parseIntresult[3],16
}:null;
}