All files / blacktrigram/src/utils colorUtils.ts

0% Statements 0/94
100% Branches 1/1
100% Functions 1/1
0% Lines 0/94

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                                                                                                                                                                                                                                                                                                         
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 (for PIXI)
 */
export function getColorWithAlpha(color: number, alpha: number): number {
  return (Math.floor(alpha * 255) << 24) | color;
}
 
/**
 * Convert PIXI color to standard hex
 */
export function pixiToHex(pixiColor: number): string {
  return `#${pixiColor.toString(16).padStart(6, "0")}`;
}
 
/**
 * 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;
  }
}
 
// DO NOT ADD ANY MORE FUNCTIONS BELOW THIS LINE
// All functions are already exported above using individual export statements