Files
star-erp/resources/js/utils/colorUtils.ts

130 lines
3.9 KiB
TypeScript
Raw Normal View History

/**
* 調
* @param hex HEX "#01ab83"
* @param percent 調
* @returns 調 HEX
*/
export function adjustBrightness(hex: string, percent: number): string {
// 移除 # 符號
const cleanHex = hex.replace('#', '');
// 解析 RGB
const num = parseInt(cleanHex, 16);
const r = (num >> 16) & 0xFF;
const g = (num >> 8) & 0xFF;
const b = num & 0xFF;
// 計算調整後的 RGB確保在 0-255 範圍內)
const newR = Math.min(255, Math.max(0, r + percent * 2.55));
const newG = Math.min(255, Math.max(0, g + percent * 2.55));
const newB = Math.min(255, Math.max(0, b + percent * 2.55));
// 轉回 HEX
const result = ((Math.round(newR) << 16) | (Math.round(newG) << 8) | Math.round(newB))
.toString(16)
.padStart(6, '0');
return `#${result}`;
}
/**
* primary-lightest: #e6f7f3
* 調
* @param hex HEX
* @returns HEX
*/
export function generateLightestColor(hex: string): string {
const cleanHex = hex.replace('#', '');
const num = parseInt(cleanHex, 16);
const r = (num >> 16) & 0xFF;
const g = (num >> 8) & 0xFF;
const b = num & 0xFF;
// 混合白色來創造非常淺的色調90% 白色 + 10% 原色)
const mixRatio = 0.1;
const newR = Math.round(255 * (1 - mixRatio) + r * mixRatio);
const newG = Math.round(255 * (1 - mixRatio) + g * mixRatio);
const newB = Math.round(255 * (1 - mixRatio) + b * mixRatio);
const result = ((newR << 16) | (newG << 8) | newB)
.toString(16)
.padStart(6, '0');
return `#${result}`;
}
/**
* primary-light: #33bc9a
* @param hex HEX
* @returns HEX
*/
export function generateLightColor(hex: string): string {
const cleanHex = hex.replace('#', '');
const num = parseInt(cleanHex, 16);
const r = (num >> 16) & 0xFF;
const g = (num >> 8) & 0xFF;
const b = num & 0xFF;
// 混合白色70% 原色 + 30% 白色)
const mixRatio = 0.7;
const newR = Math.round(r * mixRatio + 255 * (1 - mixRatio));
const newG = Math.round(g * mixRatio + 255 * (1 - mixRatio));
const newB = Math.round(b * mixRatio + 255 * (1 - mixRatio));
const result = ((newR << 16) | (newG << 8) | newB)
.toString(16)
.padStart(6, '0');
return `#${result}`;
}
/**
* primary-dark: #018a6a
* @param hex HEX
* @returns HEX
*/
export function generateDarkColor(hex: string): string {
const cleanHex = hex.replace('#', '');
const num = parseInt(cleanHex, 16);
const r = (num >> 16) & 0xFF;
const g = (num >> 8) & 0xFF;
const b = num & 0xFF;
// 降低亮度80% 原色)
const factor = 0.8;
const newR = Math.round(r * factor);
const newG = Math.round(g * factor);
const newB = Math.round(b * factor);
const result = ((newR << 16) | (newG << 8) | newB)
.toString(16)
.padStart(6, '0');
return `#${result}`;
}
/**
* active dark 60%
* @param hex HEX
* @returns HEX
*/
export function generateActiveColor(hex: string): string {
const cleanHex = hex.replace('#', '');
const num = parseInt(cleanHex, 16);
const r = (num >> 16) & 0xFF;
const g = (num >> 8) & 0xFF;
const b = num & 0xFF;
// 降低亮度60% 原色)
const factor = 0.6;
const newR = Math.round(r * factor);
const newG = Math.round(g * factor);
const newB = Math.round(b * factor);
const result = ((newR << 16) | (newG << 8) | newB)
.toString(16)
.padStart(6, '0');
return `#${result}`;
}