Spaces:
Sleeping
Sleeping
File size: 3,725 Bytes
6f6e572 |
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 |
"""
Utility Functions Module
Provides background color calculation, style generation, and other auxiliary functions.
"""
from typing import Tuple
def calculate_bg_color(player_score: int, ai_score: int) -> str:
"""
Calculate background color based on player and AI scores
Rules:
- Player leading: Green gradient (light green to dark green)
- AI leading: Red gradient (light red to dark red)
- Tie: White
Args:
player_score: Player score
ai_score: AI score
Returns:
RGB color string in format "rgb(r, g, b)"
"""
# Return white for tie
if player_score == ai_score:
return "rgb(255, 255, 255)"
# Calculate score difference
score_diff = player_score - ai_score
# Player leading - green gradient
if score_diff > 0:
# Higher difference, darker green
# Lightest: rgb(230, 255, 230) - difference of 1
# Darkest: rgb(144, 238, 144) - difference of 5+
intensity = min(score_diff, 5) # Limit max difference to 5
# Linear interpolation for RGB values
r = int(230 - (86 * intensity / 5)) # 230 -> 144
g = int(255 - (17 * intensity / 5)) # 255 -> 238
b = int(230 - (86 * intensity / 5)) # 230 -> 144
return f"rgb({r}, {g}, {b})"
# AI leading - red gradient
else:
# Higher difference, darker red
# Lightest: rgb(255, 230, 230) - difference of 1
# Darkest: rgb(255, 182, 193) - difference of 5+
intensity = min(abs(score_diff), 5) # Limit max difference to 5
# Linear interpolation for RGB values
r = 255 # Keep red channel at maximum
g = int(230 - (48 * intensity / 5)) # 230 -> 182
b = int(230 - (37 * intensity / 5)) # 230 -> 193
return f"rgb({r}, {g}, {b})"
def get_background_style(player_score: int, ai_score: int) -> str:
"""
Generate complete background style CSS string
Args:
player_score: Player score
ai_score: AI score
Returns:
CSS style string including background color and transition effect
"""
bg_color = calculate_bg_color(player_score, ai_score)
return f"""
background-color: {bg_color};
transition: background-color 0.5s ease;
"""
def format_confidence(confidence: float) -> str:
"""
Format confidence as percentage string
Args:
confidence: Confidence value between 0.0-1.0
Returns:
Formatted percentage string, e.g. "85.3%"
"""
return f"{confidence * 100:.1f}%"
def validate_image_path(path: str) -> bool:
"""
Validate if image path is valid
Args:
path: Image file path
Returns:
True if path is valid and file exists, otherwise False
"""
import os
from pathlib import Path
if not path:
return False
file_path = Path(path)
# Check if file exists
if not file_path.exists() or not file_path.is_file():
return False
# Check file extension
valid_extensions = {'.jpg', '.jpeg', '.png', '.webp', '.bmp'}
if file_path.suffix.lower() not in valid_extensions:
return False
return True
def get_score_diff_description(player_score: int, ai_score: int) -> str:
"""
Get text description of score difference
Args:
player_score: Player score
ai_score: AI score
Returns:
Text describing score status, e.g. "You're ahead by 3 points"
"""
diff = player_score - ai_score
if diff == 0:
return "Tie"
elif diff > 0:
return f"You're ahead by {diff} point{'s' if diff > 1 else ''}"
else:
return f"AI is ahead by {abs(diff)} point{'s' if abs(diff) > 1 else ''}"
|