All files / lib/components/Game/Screen/VisualNovel utils.ts

0% Statements 0/17
0% Branches 0/14
0% Functions 0/5
0% Lines 0/14

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                                                                       
import { Conversation, VisualNovelScreen } from '@uniquegood/realworld-web-interface/lib/models/game/Screen/VisualNovel';
 
export function calculateBackground(
  screenStyle: VisualNovelScreen['style'],
  conversationScreenStyle: Conversation['style']
) {
  const backgroundColor = conversationScreenStyle?.backgroundColor ?? screenStyle.backgroundColor;
 
  if (backgroundColor && screenStyle.backgroundImageUrl)
    return `url(${screenStyle.backgroundImageUrl}), ${backgroundColor}`;
 
  if (backgroundColor) return backgroundColor;
  return `url(${screenStyle.backgroundImageUrl})`;
}
 
export function calculateTextColorFromBackgroundColor(backgroundColor: string | undefined) {
  if (backgroundColor === undefined) return '#000000';
 
  const [r, g, b] = hexToRgb(backgroundColor);
  const isCloseToBrightColor = (r + g + b) / 3 > 127;
 
  return isCloseToBrightColor ? '#000000' : '#ffffff';
}
 
function hexToRgb(hex: string) {
  const rgb = hex
    .replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (m, r, g, b) => `#${r + r + g + g + b + b}`)
    .substring(1)
    .match(/.{2}/g)
    ?.map((x) => parseInt(x, 16));
 
  if (!rgb) throw Error('입력한 값은 hex 문자열 패턴을 띄지 않습니다.');
 
  return rgb;
}