emg-colorpicker
Version:
**emg-color-picker** is a custom color picker built with Svelte that can replace the default browser color picker. It currently supports hexa values (with opacity) and gradients, with plans to add support for RGB and other formats. Saved colors are stored
56 lines (44 loc) • 1.38 kB
JavaScript
//@ts-nocheck
import {handleTextColor, deepClone} from "./api"
export class GradientColor{
constructor(gradient){
this.gradient = gradient
this.selectedGradient = 0
this.init()
}
init(){
this.gradient.forEach(element => {
element.color = handleTextColor(element.color)
});
}
setGradient(gradient){
this.gradient = deepClone(gradient)
this.init()
}
setSelectedGradient(index){
this.selectedGradient = index
}
setSelectedColor(newColor){
this.gradient[this.selectedGradient].color = newColor
}
setSelectedColorOpacity(newOpacity){
this.gradient[this.selectedGradient].color = this.getSelectedColorShort()+newOpacity.toLocaleUpperCase()
}
getSelectedColor(){
return this.gradient[this.selectedGradient].color
}
getSelectedColorShort(){
return this.gradient[this.selectedGradient].color.substring(0,7)
}
getSelectedColorOpacity(){
return this.gradient[this.selectedGradient].color.substring(7,10)
}
getCssGradient(){
let linearCss = 'linear-gradient(90deg'
for(let i =0; i < this.gradient.length; i++){
linearCss += ','+this.gradient[i].color+' '+this.gradient[i].pourcentage+'%'
}
linearCss += ')'
return linearCss
}
}