All files / utils colorUtils.ts

21.42% Statements 6/28
18.18% Branches 2/11
36.36% Functions 4/11
21.42% Lines 6/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                            3755x                                                                                         84x                                             84x                                                                                                                               3755x 3755x                       378x          
import type { PlayerArchetype } from "../types/common";
import { KOREAN_COLORS } from "../types/constants/colors";
 
/**
 * Convert RGB components to hex color
 */
export function rgbToHex(r: number, g: number, b: number): number {
  return ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
}
 
/**
 * Convert hex color to RGB components
 */
export function hexToRgb(hex: number): { r: number; g: number; b: number } {
  return {
    r: (hex >> 16) & 0xff,
    g: (hex >> 8) & 0xff,
    b: hex & 0xff,
  };
}
 
/**
 * Darken a color by a specified amount
 */
export function darkenColor(color: number, amount: number = 0.1): number {
  const { r, g, b } = hexToRgb(color);
  const factor = Math.max(0, 1 - amount);
 
  return rgbToHex(
    Math.floor(r * factor),
    Math.floor(g * factor),
    Math.floor(b * factor)
  );
}
 
/**
 * Lighten a color by a specified amount
 */
export function lightenColor(color: number, amount: number = 0.1): number {
  const { r, g, b } = hexToRgb(color);
  const factor = Math.min(1, amount);
 
  return rgbToHex(
    Math.min(255, Math.floor(r + (255 - r) * factor)),
    Math.min(255, Math.floor(g + (255 - g) * factor)),
    Math.min(255, Math.floor(b + (255 - b) * factor))
  );
}
 
/**
 * Get archetype colors
 */
export function getArchetypeColors(archetype: PlayerArchetype): {
  primary: number;
  secondary: number;
} {
  const colorMap: Record<
    PlayerArchetype,
    { primary: number; secondary: number }
  > = {
    musa: {
      primary: KOREAN_COLORS.ACCENT_GOLD,
      secondary: KOREAN_COLORS.SECONDARY_BROWN_DARK, // Fix: Use SECONDARY_BROWN_DARK
    },
    amsalja: {
      primary: KOREAN_COLORS.PRIMARY_RED, // Fix: Use PRIMARY_RED instead of PRIMARY_PURPLE
      secondary: KOREAN_COLORS.UI_STEEL_GRAY,
    },
    hacker: {
      primary: KOREAN_COLORS.PRIMARY_CYAN,
      secondary: KOREAN_COLORS.ACCENT_BLUE,
    },
    jeongbo_yowon: {
      primary: KOREAN_COLORS.POSITIVE_GREEN,
      secondary: KOREAN_COLORS.UI_STEEL_GRAY_DARK,
    },
    jojik_pokryeokbae: {
      primary: KOREAN_COLORS.ACCENT_RED,
      secondary: KOREAN_COLORS.SECONDARY_BROWN_DARK, // Fix: Use SECONDARY_BROWN_DARK
    },
  };
 
  return (
    colorMap[archetype] || {
      primary: KOREAN_COLORS.WHITE_SOLID,
      secondary: KOREAN_COLORS.UI_STEEL_GRAY,
    }
  );
}
 
/**
 * Interpolate between two colors
 */
export function interpolateColor(
  color1: number,
  color2: number,
  factor: number
): number {
  const rgb1 = hexToRgb(color1);
  const rgb2 = hexToRgb(color2);
 
  const r = Math.floor(rgb1.r + (rgb2.r - rgb1.r) * factor);
  const g = Math.floor(rgb1.g + (rgb2.g - rgb1.g) * factor);
  const b = Math.floor(rgb1.b + (rgb2.b - rgb1.b) * factor);
 
  return rgbToHex(r, g, b);
}
 
/**
 * Get color with alpha
 */
export function getColorWithAlpha(color: number, alpha: number): number {
  return (Math.floor(alpha * 255) << 24) | color;
}
 
/**
 * Get contrast color (black or white) for given background
 */
export function getContrastColor(backgroundColor: number): number {
  const { r, g, b } = hexToRgb(backgroundColor);
  const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
  return luminance > 0.5
    ? KOREAN_COLORS.BLACK_SOLID
    : KOREAN_COLORS.WHITE_SOLID;
}
 
/**
 * Get color based on health percentage
 */
export function getHealthColor(healthPercentage: number): number {
  if (healthPercentage > 0.6) {
    return KOREAN_COLORS.POSITIVE_GREEN;
  } else if (healthPercentage > 0.3) {
    return KOREAN_COLORS.SECONDARY_YELLOW;
  } else {
    return KOREAN_COLORS.ACCENT_RED;
  }
}
 
/**
 * Convert hex color to RGBA string for CSS
 * @param hex - Hex color value (e.g., 0x1a1a1a)
 * @param alpha - Alpha value between 0 and 1 (default: 1)
 * @returns RGBA string (e.g., "rgba(26, 26, 26, 0.96)")
 */
export function hexToRgbaString(hex: number, alpha: number = 1): string {
  const { r, g, b } = hexToRgb(hex);
  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
 
/**
 * Convert numeric color to hex string for CSS
 * @param color - Numeric color value (e.g., 0x00ffff)
 * @returns Hex color string without # prefix (e.g., "00ffff")
 * @example
 * toHex(0x00ffff) // "00ffff"
 * toHex(KOREAN_COLORS.PRIMARY_CYAN) // "00ffff"
 */
export function toHex(color: number): string {
  return color.toString(16).padStart(6, '0');
}
 
// DO NOT ADD ANY MORE FUNCTIONS BELOW THIS LINE
// All functions are already exported above using individual export statements