[Harmony]颜色初始化

默认初始化颜色

TypeScript 复制代码
let color: Color = 0xFF00FF

创建一个工具,用十六进制颜色和RGBA初始化颜色

TypeScript 复制代码
// 颜色工具类
export class ColorUtils {
  /**
   * 十六进制颜色初始化(支持透明度)
   * @param hex 支持格式:#RRGGBB、#AARRGGBB、0xRRGGBB、0xAARRGGBB
   * @param alpha 可选透明度(0-1)
   */
  static fromHex(hex: string | number, alpha?: number): Color {
    let hexValue: number;

    if (typeof hex === 'string') {
      // 去除#号
      hex = hex.replace('#', '');

      // 处理3/4位简写格式
      if (hex.length === 3 || hex.length === 4) {
        hex = hex.split('').map(c => c + c).join('');
      }

      // 补全6位或8位
      if (hex.length === 6) {
        hex = 'FF' + hex; // 默认不透明
      }

      hexValue = parseInt(hex, 16);
    } else {
      hexValue = hex;
    }

    // 应用透明度覆盖
    if (alpha !== undefined) {
      const alphaByte = Math.round(alpha * 255);
      hexValue = (alphaByte << 24) | (hexValue & 0x00FFFFFF);
    }

    return hexValue as Color;
  }

  /**
   * RGBA颜色初始化
   * @param r 红色通道(0-255)
   * @param g 绿色通道(0-255)
   * @param b 蓝色通道(0-255)
   * @param a 透明度(0-1)
   */
  static fromRGBA(r: number, g: number, b: number, a: number = 1): Color {
    const alphaByte = Math.round(a * 255);
    return ((alphaByte << 24) | (r << 16) | (g << 8) | b) as Color;
  }


  /**
   * 将Color转换为十六进制字符串
   * @param color 颜色对象
   * @param withAlpha 是否包含透明度通道
   * @returns 格式:#AARRGGBB 或 #RRGGBB
   */
  static toHexString(color: Color, withAlpha: boolean = true): string {
    const num = Number(color);
    if (withAlpha) {
      return `#${((num >>> 0) & 0xFFFFFFFF).toString(16).padStart(8, '0').toUpperCase()}`;
    }
    return `#${((num & 0x00FFFFFF) >>> 0).toString(16).padStart(6, '0').toUpperCase()}`;
  }

  /**
   * 将Color转换为RGBA对象
   * @param color 颜色对象
   * @returns 包含r,g,b,a(0-1)的对象
   */
  static toRGBA(color: Color): RGBAColor {
    const num = Number(color);
    return {
      r: (num >> 16) & 0xFF,
      g: (num >> 8) & 0xFF,
      b: num & 0xFF,
      a: ((num >> 24) & 0xFF) / 255
    };
  }
}

interface RGBAColor {
  r: number;
  g: number;
  b: number;
  a: number;
}
相关推荐
俩毛豆3 分钟前
HarmonyOS APP开发-一文说清基础类型数据的非预期输入转换与兜底-《精通HarmonyOS NEXT :鸿蒙App开发入门与项目化实战》读者福利
华为·harmonyos
lbb 小魔仙29 分钟前
【Harmonyos】开源鸿蒙跨平台训练营DAY8:Flutter鸿蒙开发指南获取轮播图数据
flutter·华为·harmonyos
哈__1 小时前
ReactNative for Harmony 项目鸿蒙化三方库集成实战:react-native-svg
react native·react.js·harmonyos
熊猫钓鱼>_>1 小时前
【开源鸿蒙跨平台开发先锋训练营】Day 9:鸿蒙跨平台Tab 开发问题与列表操作难点复盘
react native·华为·开源·tab·harmonyos·rn·hvigor
哈__2 小时前
ReactNative for Harmony 项目鸿蒙化三方库集成实战:react-native-safe-area-context
react native·react.js·harmonyos
Goway_Hui2 小时前
【开源鸿蒙跨平台开发--KuiklyUI--03】KuiklyUI 入门实战:从零打造高性能跨平台 Todo 应用
华为·开源·harmonyos·kuikly
waeng_luo2 小时前
如何利用AI提高鸿蒙开发效率:从Rules到智能开发实践
人工智能·华为·harmonyos
哈__2 小时前
ReactNative for Harmony项目鸿蒙化三方库集成实战:react-native-elements
react native·react.js·harmonyos
哈__2 小时前
ReactNative for Harmony 项目鸿蒙化三方库集成实战:@react-native-ohos/react-native-picker
react native·react.js·harmonyos
ITUnicorn3 小时前
【HarmonyOS6】简易计数器开发
华为·harmonyos·arkts·鸿蒙·harmonyos6