mdn / css-examples

Code examples that accompany the MDN CSS documentation
https://developer.mozilla.org/docs/Web/CSS
Creative Commons Zero v1.0 Universal
618 stars 854 forks source link

New page: color modules example #125

Closed estelle closed 1 year ago

estelle commented 1 year ago

Example for new CSS color module page.

estelle commented 1 year ago

I added opacity * 1 to force it to a number so I could do opacity === 1. HTML form controls return strings, hence the == 1

estelle commented 1 year ago

@teoli2003 We went with

function hexOpacity(color, opacity)  {
  let char = "00";
  if (opacity > 0) {
    char = Math.floor(opacity * 255).toString(16);
  }
  return `${color}${char}`;
}

Is it also ok to go with the following way of declaring a function (the first line), or against our best practices guidelines?

const hexOpacity = (color, opacity) => {
  let char = "00";
  if (opacity > 0) {
    char = Math.floor(opacity * 255).toString(16);
  }
  return `${color}${char}`;
}
teoli2003 commented 1 year ago

@teoli2003 We went with

function hexOpacity(color, opacity)  {
  let char = "00";
  if (opacity > 0) {
    char = Math.floor(opacity * 255).toString(16);
  }
  return `${color}${char}`;
}

Is it also ok to go with the following way of declaring a function (the first line), or against our best practices guidelines?

const hexOpacity = (color, opacity) => {
  let char = "00";
  if (opacity > 0) {
    char = Math.floor(opacity * 255).toString(16);
  }
  return `${color}${char}`;
}

When possible, I prefer to have a function definition. I don't see what the longest syntax brings to readability.

(I'm not a fan of setting the else value before the if, but that's not against our rules and a matter of taste)