excaliburjs / Excalibur

🎮 Your friendly TypeScript 2D game engine for the web 🗡️
https://excaliburjs.com
BSD 2-Clause "Simplified" License
1.83k stars 192 forks source link

Add `Color.toHex()`, `toRGBA()`, and `toHSLA()` methods #852

Closed kamranayub closed 7 years ago

kamranayub commented 7 years ago

Context

Color supports converting from Hex/HSL representation but not back to them (only RGBA).

Proposal

Modify Color class:

  1. Add a new instance method toHex().
  2. Add a new instance method toRGBA()
  3. Add a new instance method toHSLA()
  4. Add an optional param type to toString() that accepts a string, either rgb, hsl or hex parameter (default: "rgb" to maintain backwards compat). Depending on value, it should call either of the three new instance methods.
htalat commented 7 years ago

hey! can i work on this?

kamranayub commented 7 years ago

@htalat Absolutely, your PR inspired this one when I noticed these were missing 👍

htalat commented 7 years ago

haha great!

htalat commented 7 years ago

what to return in case an invalid format is provided?

   public toString(format = 'rgb') {
      if (format === 'rgb')
         return this.toRGBA();

      if(format === 'hsl')
         return this.toHSLA();

      if(format === 'hex')
         return this.toHex();

      return 'invalid format';  // what would be the correct way to handle this flow?
   }
kamranayub commented 7 years ago

I would throw an error (throw new Error("foo"))

On Thu, Jun 29, 2017 at 3:27 PM Hassan Talat notifications@github.com wrote:

what to return in case an invalid format is provided?

public toString(format = 'rgb') { if (format === 'rgb') return this.toRGBA();

  if(format === 'hsl')
     return this.toHSLA();

  if(format === 'hex')
     return this.toHex();

  return 'invalid format';  // what would be the correct way to handle this flow?

}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/excaliburjs/Excalibur/issues/852#issuecomment-312094265, or mute the thread https://github.com/notifications/unsubscribe-auth/AAiaawcBnbPT-cYuQgWuQytF63WNoJ-Tks5sJAizgaJpZM4OH8Gf .