HCDD 340 — Assignment 03

Description

For this assignment, you will create a progressive web app (PWA) with JS event handling. Please use the starter file in Canvas (Files → Assignments → Assignment-03).

The video below shows an implementation of the assignment:

Deliverables

Please upload the following files (as a zip file):

Requirements & Rubrics

PWA

Event handling

Implementation suggestions

Installing PWA

Event handling

/**
 * Converts an RGB or RGBA color string to its hexadecimal equivalent.
 */
function rgbStringToHex(rgbString) {
  // Regex to match rgb() or rgba() strings with number or percentage values,
  // separated by spaces or commas. It captures up to four numeric groups.
  const regex = /rgba?\((\d+)[,\s]+(\d+)[,\s]+(\d+)[,\s/]*([\d.]+)?\)/i;
  const match = regex.exec(rgbString);

  if (!match) {
    return null; // Return null for invalid input
  }

  // Extract color components from the regex match
  const [r, g, b, a] = match.slice(1).map(Number);

  // Helper function to convert a number to a two-digit hex string
  const toHex = (c) => c.toString(16).padStart(2, '0');

  // Convert RGB components to hex and join them
  const hex = `${toHex(r)}${toHex(g)}${toHex(b)}`;

  // If an alpha value exists, convert it to a two-digit hex and append it
  const hexAlpha = a !== undefined && !isNaN(a) ? toHex(Math.round(a * 255)) : '';

  return `#${hex}${hexAlpha}`;
}

/**
 * Gets color (in Hex) for a given element
 */
const getTextColor = (element) => {
    const computedStyle = window.getComputedStyle(element);
    const textColor = computedStyle.getPropertyValue("color");
    return rgbStringToHex(textColor);
}