Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
-
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
import requests
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import json
|
| 7 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
@@ -17,6 +19,10 @@ app.add_middleware(
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
class WebsiteURL(BaseModel):
|
| 21 |
url: str
|
| 22 |
|
|
@@ -24,77 +30,390 @@ class WebsiteURL(BaseModel):
|
|
| 24 |
async def extract_website(website_url: WebsiteURL):
|
| 25 |
try:
|
| 26 |
# Fetch the website content
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
response.raise_for_status()
|
| 29 |
|
| 30 |
# Parse the HTML
|
| 31 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
elements = []
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
if
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
import requests
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
import json
|
| 6 |
from pydantic import BaseModel
|
| 7 |
+
from urllib.parse import urljoin
|
| 8 |
+
import logging
|
| 9 |
+
import re
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Configure logging
|
| 23 |
+
logging.basicConfig(level=logging.INFO)
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
class WebsiteURL(BaseModel):
|
| 27 |
url: str
|
| 28 |
|
|
|
|
| 30 |
async def extract_website(website_url: WebsiteURL):
|
| 31 |
try:
|
| 32 |
# Fetch the website content
|
| 33 |
+
headers = {
|
| 34 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 35 |
+
}
|
| 36 |
+
response = requests.get(website_url.url, headers=headers, timeout=15)
|
| 37 |
response.raise_for_status()
|
| 38 |
|
| 39 |
# Parse the HTML
|
| 40 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 41 |
|
| 42 |
+
# Fetch external stylesheets
|
| 43 |
+
for link in soup.find_all('link', rel='stylesheet'):
|
| 44 |
+
href = link.get('href')
|
| 45 |
+
if href:
|
| 46 |
+
css_url = urljoin(website_url.url, href)
|
| 47 |
+
try:
|
| 48 |
+
css_response = requests.get(css_url, headers=headers, timeout=5)
|
| 49 |
+
css_response.raise_for_status()
|
| 50 |
+
css_soup = BeautifulSoup(f"<style>{css_response.text}</style>", 'html.parser')
|
| 51 |
+
soup.append(css_soup)
|
| 52 |
+
logger.info(f"Fetched stylesheet: {css_url}")
|
| 53 |
+
except requests.RequestException as e:
|
| 54 |
+
logger.warning(f"Failed to fetch stylesheet {css_url}: {e}")
|
| 55 |
+
|
| 56 |
elements = []
|
| 57 |
+
y_offset = 20
|
| 58 |
+
element_index = 0
|
| 59 |
+
|
| 60 |
+
def parse_styles(style_text):
|
| 61 |
+
styles = {}
|
| 62 |
+
try:
|
| 63 |
+
for style_item in style_text.split(';'):
|
| 64 |
+
if ':' in style_item:
|
| 65 |
+
prop, val = style_item.split(':', 1)
|
| 66 |
+
styles[prop.strip()] = val.strip()
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logger.warning(f"Error parsing styles: {e}")
|
| 69 |
+
return styles
|
| 70 |
+
|
| 71 |
+
def get_styles(element):
|
| 72 |
+
styles = parse_styles(element.get('style', ''))
|
| 73 |
+
# Process <style> tags
|
| 74 |
+
for style_tag in soup.find_all('style'):
|
| 75 |
+
try:
|
| 76 |
+
css = style_tag.string or ''
|
| 77 |
+
for rule in css.split('}'):
|
| 78 |
+
if '{' in rule:
|
| 79 |
+
selector, props = rule.split('{', 1)
|
| 80 |
+
selector = selector.strip()
|
| 81 |
+
if selector.startswith('.') and 'class' in element.attrs:
|
| 82 |
+
classes = element.get('class', [])
|
| 83 |
+
if selector[1:] in classes:
|
| 84 |
+
styles.update(parse_styles(props))
|
| 85 |
+
elif selector.startswith('#') and element.get('id') == selector[1:]:
|
| 86 |
+
styles.update(parse_styles(props))
|
| 87 |
+
elif selector.lower() == element.name:
|
| 88 |
+
styles.update(parse_styles(props))
|
| 89 |
+
except Exception as e:
|
| 90 |
+
logger.warning(f"Error parsing <style> tag: {e}")
|
| 91 |
+
return styles
|
| 92 |
+
|
| 93 |
+
def safe_int(value, default=100):
|
| 94 |
+
if not value:
|
| 95 |
+
return default
|
| 96 |
+
value = re.sub(r'[^\d]', '', value)
|
| 97 |
+
try:
|
| 98 |
+
return int(value) if value else default
|
| 99 |
+
except ValueError:
|
| 100 |
+
logger.warning(f"Invalid value for int conversion: {value}")
|
| 101 |
+
return default
|
| 102 |
+
|
| 103 |
+
def parse_color(color):
|
| 104 |
+
if not color:
|
| 105 |
+
return '#FFFFFF'
|
| 106 |
+
color = color.lower()
|
| 107 |
+
if color.startswith('#'):
|
| 108 |
+
return color
|
| 109 |
+
if color.startswith('rgb'):
|
| 110 |
+
try:
|
| 111 |
+
nums = re.findall(r'\d+', color)
|
| 112 |
+
r, g, b = map(int, nums[:3])
|
| 113 |
+
return f'#{r:02x}{g:02x}{b:02x}'
|
| 114 |
+
except:
|
| 115 |
+
return '#FFFFFF'
|
| 116 |
+
return '#FFFFFF'
|
| 117 |
+
|
| 118 |
+
def process_element(tag, parent_id=None, depth=0):
|
| 119 |
+
nonlocal elements, y_offset, element_index
|
| 120 |
+
styles = get_styles(tag)
|
| 121 |
+
logger.info(f"Processing tag: {tag.name}, text: {tag.get_text(strip=True)[:50]}")
|
| 122 |
+
element = {
|
| 123 |
+
'id': f'element-{element_index}',
|
| 124 |
+
'x': safe_int(styles.get('left', styles.get('margin-left', '0px')), 20 + depth * 10),
|
| 125 |
+
'y': safe_int(styles.get('top', styles.get('margin-top', '0px')), y_offset),
|
| 126 |
+
'parentId': parent_id
|
| 127 |
+
}
|
| 128 |
+
element_index += 1
|
| 129 |
+
|
| 130 |
+
if tag.name in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a', 'button']:
|
| 131 |
+
text = tag.get_text(strip=True)
|
| 132 |
+
if text:
|
| 133 |
+
font_size = safe_int(styles.get('font-size', '16px'), 16)
|
| 134 |
+
element.update({
|
| 135 |
+
'type': 'text',
|
| 136 |
+
'tag': tag.name,
|
| 137 |
+
'content': text[:1000] or 'Text Placeholder',
|
| 138 |
+
'fontSize': font_size,
|
| 139 |
+
'color': parse_color(styles.get('color', '#000000')),
|
| 140 |
+
'fontFamily': styles.get('font-family', 'Inter'),
|
| 141 |
+
'fontWeight': styles.get('font-weight', '400')
|
| 142 |
})
|
| 143 |
+
y_offset += font_size + 20
|
| 144 |
+
elements.append(element)
|
| 145 |
+
logger.info(f"Added text element: {text[:50]}")
|
| 146 |
+
|
| 147 |
+
elif tag.name == 'img':
|
| 148 |
+
src = tag.get('src', '')
|
| 149 |
+
if src:
|
| 150 |
+
src = urljoin(website_url.url, src)
|
| 151 |
+
width = safe_int(tag.get('width', styles.get('width', '200px')), 200)
|
| 152 |
+
height = safe_int(tag.get('height', styles.get('height', '200px')), 200)
|
| 153 |
+
element.update({
|
| 154 |
+
'type': 'image',
|
| 155 |
+
'src': src,
|
| 156 |
+
'alt': tag.get('alt', ''),
|
| 157 |
+
'width': max(width, 20),
|
| 158 |
+
'height': max(height, 20)
|
| 159 |
+
})
|
| 160 |
+
y_offset += height + 20
|
| 161 |
+
elements.append(element)
|
| 162 |
+
logger.info(f"Added image element: {src[:50]}")
|
| 163 |
+
|
| 164 |
+
elif tag.name == 'div':
|
| 165 |
+
width = safe_int(styles.get('width', '300px'), 300)
|
| 166 |
+
height = safe_int(styles.get('height', '100px'), 100)
|
| 167 |
+
element.update({
|
| 168 |
+
'type': 'div',
|
| 169 |
+
'width': max(width, 20),
|
| 170 |
+
'height': max(height, 20),
|
| 171 |
+
'backgroundColor': parse_color(styles.get('background-color', '#FFFFFF')),
|
| 172 |
+
'borderWidth': safe_int(styles.get('border-width', '0px'), 0),
|
| 173 |
+
'borderColor': parse pregiud
|
| 174 |
+
|
| 175 |
+
1. In the **Files** tab, open `app.py`.
|
| 176 |
+
2. Replace its content with the above code.
|
| 177 |
+
3. Click **Commit changes** (message: "Enhance app.py parsing").
|
| 178 |
+
|
| 179 |
+
**Changes**:
|
| 180 |
+
- Added external stylesheet fetching (`<link>` tags).
|
| 181 |
+
- Improved `<style>` tag parsing to apply CSS rules based on class, ID, or tag.
|
| 182 |
+
- Added logging to debug parsed elements.
|
| 183 |
+
- Included `div` elements for better layout representation.
|
| 184 |
+
- Used `urljoin` for consistent image URL handling.
|
| 185 |
+
|
| 186 |
+
#### Step 3: Update `code.js`
|
| 187 |
+
Enhance Figma rendering to respect element positions, hierarchies, and styles.
|
| 188 |
+
|
| 189 |
+
```javascript
|
| 190 |
+
figma.showUI(__html__, { width: 340, height: 320 });
|
| 191 |
+
|
| 192 |
+
figma.ui.onmessage = async (msg) => {
|
| 193 |
+
if (msg.type === 'convert-website') {
|
| 194 |
+
try {
|
| 195 |
+
const apiUrl = "https://<your-space-name>.hf.space/extract";
|
| 196 |
+
console.log('Sending request to:', apiUrl, 'with URL:', msg.url);
|
| 197 |
+
|
| 198 |
+
const response = await fetch(apiUrl, {
|
| 199 |
+
method: 'POST',
|
| 200 |
+
headers: { 'Content-Type': 'application/json' },
|
| 201 |
+
body: JSON.stringify({ url: msg.url })
|
| 202 |
+
});
|
| 203 |
+
|
| 204 |
+
if (!response.ok) {
|
| 205 |
+
const errorText = await response.text();
|
| 206 |
+
console.log('Error response:', errorText);
|
| 207 |
+
throw new Error(`Server responded with ${response.status}: ${errorText}`);
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
const data = await response.json();
|
| 211 |
+
console.log('Received data:', data);
|
| 212 |
+
|
| 213 |
+
if (!data.elements || data.elements.length === 0) {
|
| 214 |
+
figma.ui.postMessage({
|
| 215 |
+
type: 'conversion-error',
|
| 216 |
+
error: 'No elements found. The site may rely on JavaScript.'
|
| 217 |
+
});
|
| 218 |
+
return;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
const page = figma.createPage();
|
| 222 |
+
page.name = `Web Import: ${new URL(msg.url).hostname}`;
|
| 223 |
+
figma.currentPage = page;
|
| 224 |
+
|
| 225 |
+
const frame = figma.createFrame();
|
| 226 |
+
frame.name = "Website Frame";
|
| 227 |
+
frame.resize(1440, 900);
|
| 228 |
+
frame.fills = [{ type: 'SOLID', color: { r: 1, g: 1, b: 1 } }];
|
| 229 |
+
|
| 230 |
+
const nodeMap = new Map();
|
| 231 |
+
|
| 232 |
+
for (const element of data.elements) {
|
| 233 |
+
let parentNode = element.parentId ? nodeMap.get(element.parentId) : frame;
|
| 234 |
+
|
| 235 |
+
if (!parentNode) {
|
| 236 |
+
console.log('Parent not found for:', element.id, 'using main frame');
|
| 237 |
+
parentNode = frame;
|
| 238 |
}
|
| 239 |
+
|
| 240 |
+
if (element.type === 'text') {
|
| 241 |
+
try {
|
| 242 |
+
const textNode = figma.createText();
|
| 243 |
+
const fontFamily = element.fontFamily || 'Inter';
|
| 244 |
+
const fontStyle = element.fontWeight === '700' ? 'Bold' : 'Regular';
|
| 245 |
+
await figma.loadFontAsync({ family: fontFamily, style: fontStyle }).catch(() =>
|
| 246 |
+
figma.loadFontAsync({ family: 'Inter', style: fontStyle })
|
| 247 |
+
);
|
| 248 |
+
textNode.fontName = { family: fontFamily, style: fontStyle };
|
| 249 |
+
textNode.characters = element.content || 'Text Placeholder';
|
| 250 |
+
textNode.fontSize = element.fontSize || 16;
|
| 251 |
+
textNode.x = element.x || 100;
|
| 252 |
+
textNode.y = element.y || 100;
|
| 253 |
+
textNode.fills = [{ type: 'SOLID', color: hexToRgb(element.color || '#000000') }];
|
| 254 |
+
|
| 255 |
+
switch (element.tag) {
|
| 256 |
+
case 'h1': textNode.fontSize = 32; break;
|
| 257 |
+
case 'h2': textNode.fontSize = 28; break;
|
| 258 |
+
case 'h3': textNode.fontSize = 24; break;
|
| 259 |
+
case 'h4': textNode.fontSize = 20; break;
|
| 260 |
+
case 'h5': textNode.fontSize = 18; break;
|
| 261 |
+
case 'h6': textNode.fontSize = 16; break;
|
| 262 |
+
case 'a': textNode.fills = [{ type: 'SOLID', color: { r: 0, g: 0.439, b: 0.866 } }]; break;
|
| 263 |
+
case 'button':
|
| 264 |
+
const buttonRect = figma.createRectangle();
|
| 265 |
+
buttonRect.resize(textNode.width + 40, textNode.height + 20);
|
| 266 |
+
buttonRect.x = textNode.x;
|
| 267 |
+
buttonRect.y = textNode.y;
|
| 268 |
+
buttonRect.cornerRadius = 8;
|
| 269 |
+
buttonRect.fills = [{ type: 'SOLID', color: { r: 0.094, g: 0.627, b: 0.984 } }];
|
| 270 |
+
textNode.fills = [{ type: 'SOLID', color: { r: 1, g: 1, b: 1 } }];
|
| 271 |
+
textNode.x += 20;
|
| 272 |
+
textNode.y += 10;
|
| 273 |
+
parentNode.appendChild(buttonRect);
|
| 274 |
+
break;
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
parentNode.appendChild(textNode);
|
| 278 |
+
nodeMap.set(element.id, textNode);
|
| 279 |
+
console.log('Added text:', element.content);
|
| 280 |
+
} catch (error) {
|
| 281 |
+
console.error('Error creating text:', error);
|
| 282 |
+
}
|
| 283 |
+
} else if (element.type === 'image') {
|
| 284 |
+
try {
|
| 285 |
+
const rect = figma.createRectangle();
|
| 286 |
+
rect.name = `Image: ${element.alt || 'Untitled'}`;
|
| 287 |
+
rect.resize(element.width || 300, element.height || 200);
|
| 288 |
+
rect.x = element.x || 100;
|
| 289 |
+
rect.y = element.y || 100;
|
| 290 |
+
|
| 291 |
+
if (element.src) {
|
| 292 |
+
try {
|
| 293 |
+
const imageData = await fetchImage(element.src);
|
| 294 |
+
const figmaImage = figma.createImage(imageData);
|
| 295 |
+
rect.fills = [{ type: 'IMAGE', imageHash: figmaImage.hash, scaleMode: 'FILL' }];
|
| 296 |
+
} catch (e) {
|
| 297 |
+
console.error('Failed to load image:', element.src, e);
|
| 298 |
+
rect.fills = [{ type: 'SOLID', color: { r: 0.9, g: 0.9, b: 0.9 } }];
|
| 299 |
+
const label = figma.createText();
|
| 300 |
+
await figma.loadFontAsync({ family: 'Inter', style: 'Regular' });
|
| 301 |
+
label.characters = `[Image: ${element.alt || element.src.split('/').pop()}]`;
|
| 302 |
+
label.fontSize = 12;
|
| 303 |
+
label.x = rect.x + rect.width / 2 - label.width / 2;
|
| 304 |
+
label.y = rect.y + rect.height / 2 - label.height / 2;
|
| 305 |
+
parentNode.appendChild(label);
|
| 306 |
+
}
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
parentNode.appendChild(rect);
|
| 310 |
+
nodeMap.set(element.id, rect);
|
| 311 |
+
console.log('Added image:', element.src);
|
| 312 |
+
} catch (error) {
|
| 313 |
+
console.error('Error creating image:', error);
|
| 314 |
+
}
|
| 315 |
+
} else if (element.type === 'div') {
|
| 316 |
+
try {
|
| 317 |
+
const group = figma.createFrame();
|
| 318 |
+
group.name = 'Div';
|
| 319 |
+
group.resize(element.width || 300, element.height || 100);
|
| 320 |
+
group.x = element.x || 100;
|
| 321 |
+
group.y = element.y || 100;
|
| 322 |
+
group.fills = [{ type: 'SOLID', color: hexToRgb(element.backgroundColor || '#FFFFFF') }];
|
| 323 |
+
if (element.borderWidth > 0) {
|
| 324 |
+
group.strokes = [{ type: 'SOLID', color: hexToRgb(element.borderColor || '#000000') }];
|
| 325 |
+
group.strokeWeight = element.borderWidth;
|
| 326 |
+
}
|
| 327 |
+
parentNode.appendChild(group);
|
| 328 |
+
nodeMap.set(element.id, group);
|
| 329 |
+
console.log('Added div:', element.width, element.height);
|
| 330 |
+
} catch (error) {
|
| 331 |
+
console.error('Error creating div:', error);
|
| 332 |
+
}
|
| 333 |
}
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
if (data.colors && data.colors.length > 0) {
|
| 337 |
+
const colorSection = figma.createFrame();
|
| 338 |
+
colorSection.name = "Color Palette";
|
| 339 |
+
colorSection.resize(300, 50 + data.colors.length * 50);
|
| 340 |
+
colorSection.x = frame.width - 400;
|
| 341 |
+
colorSection.y = 100;
|
| 342 |
+
|
| 343 |
+
const colorTitle = figma.createText();
|
| 344 |
+
await figma.loadFontAsync({ family: "Inter", style: "Medium" });
|
| 345 |
+
colorTitle.fontName = { family: "Inter", style: "Medium" };
|
| 346 |
+
colorTitle.characters = "Color Palette";
|
| 347 |
+
colorTitle.fontSize = 16;
|
| 348 |
+
colorTitle.x = 20;
|
| 349 |
+
colorTitle.y = 20;
|
| 350 |
+
colorSection.appendChild(colorTitle);
|
| 351 |
+
|
| 352 |
+
let colorY = 60;
|
| 353 |
+
for (const colorStr of data.colors) {
|
| 354 |
+
try {
|
| 355 |
+
const { r, g, b } = hexToRgb(colorStr);
|
| 356 |
+
const colorRect = figma.createRectangle();
|
| 357 |
+
colorRect.resize(30, 30);
|
| 358 |
+
colorRect.x = 20;
|
| 359 |
+
colorRect.y = colorY;
|
| 360 |
+
colorRect.fills = [{ type: 'SOLID', color: { r, g, b } }];
|
| 361 |
+
|
| 362 |
+
const colorLabel = figma.createText();
|
| 363 |
+
await figma.loadFontAsync({ family: "Inter", style: "Regular" });
|
| 364 |
+
colorLabel.fontName = { family: "Inter", style: "Regular" };
|
| 365 |
+
colorLabel.characters = colorStr;
|
| 366 |
+
colorLabel.fontSize = 12;
|
| 367 |
+
colorLabel.x = 60;
|
| 368 |
+
colorLabel.y = colorY + 8;
|
| 369 |
+
|
| 370 |
+
colorSection.appendChild(colorRect);
|
| 371 |
+
colorSection.appendChild(colorLabel);
|
| 372 |
+
colorY += 40;
|
| 373 |
+
} catch (error) {
|
| 374 |
+
console.error("Error creating color swatch:", error);
|
| 375 |
+
}
|
| 376 |
+
}
|
| 377 |
+
frame.appendChild(colorSection);
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
+
const bounds = frame.children.reduce(
|
| 381 |
+
(acc, node) => ({
|
| 382 |
+
width: Math.max(acc.width, node.x + node.width),
|
| 383 |
+
height: Math.max(acc.height, node.y + node.height)
|
| 384 |
+
}),
|
| 385 |
+
{ width: 1440, height: 900 }
|
| 386 |
+
);
|
| 387 |
+
frame.resize(bounds.width + 20, bounds.height + 20);
|
| 388 |
+
figma.viewport.scrollAndZoomIntoView([frame]);
|
| 389 |
+
|
| 390 |
+
figma.ui.postMessage({ type: 'conversion-complete' });
|
| 391 |
+
} catch (error) {
|
| 392 |
+
console.error("Conversion error:", error);
|
| 393 |
+
figma.ui.postMessage({
|
| 394 |
+
type: 'conversion-error',
|
| 395 |
+
error: error.message
|
| 396 |
+
});
|
| 397 |
+
}
|
| 398 |
+
}
|
| 399 |
+
};
|
| 400 |
+
|
| 401 |
+
function hexToRgb(hex) {
|
| 402 |
+
hex = hex.replace('#', '');
|
| 403 |
+
const r = parseInt(hex.length === 3 ? hex[0] + hex[0] : hex.slice(0, 2), 16) / 255;
|
| 404 |
+
const g = parseInt(hex.length === 3 ? hex[1] + hex[1] : hex.slice(2, 4), 16) / 255;
|
| 405 |
+
const b = parseInt(hex.length === 3 ? hex[2] + hex[2] : hex.slice(4, 6), 16) / 255;
|
| 406 |
+
return { r, g, b };
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
async function fetchImage(url) {
|
| 410 |
+
try {
|
| 411 |
+
const response = await fetch(url);
|
| 412 |
+
if (!response.ok) throw new Error(`Failed to fetch image: ${response.status}`);
|
| 413 |
+
const arrayBuffer = await response.arrayBuffer();
|
| 414 |
+
return new Uint8Array(arrayBuffer);
|
| 415 |
+
} catch (e) {
|
| 416 |
+
console.error('Image fetch error:', e);
|
| 417 |
+
throw e;
|
| 418 |
+
}
|
| 419 |
+
}
|