3

I've seen some implementations for converting RGB to HSL. Most are accurate and work in both directions.

To me its not important that it will work in 2 directions (no need to put back to RGB) But i want code that returns values from 0 to 255 max, also for the Hue channel. And I wouldnt like to do devisions like Hue/360*250 i am searching for integer based math no Dwords (its for another system), nice would be some kind of boolean logix (and/or/xor)

It should not do any integer or real number based math, the goal is code working only using byte math.

Maybe someone already has found such math when he used code like

  1. c++ or
  2. c# or
  3. python

Which i would be able to translate to c++

user613326
  • 2,140
  • 9
  • 34
  • 63
  • 2
    possible duplicate of [HSL to RGB color conversion](http://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion) – Mark Ransom Oct 08 '12 at 21:54

2 Answers2

5

Checkout the colorsys module, it has methods like:

colorsys.rgb_to_hls(r,g,b)

colorsys.hls_to_rgb(h,l,s)

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    You'll need to divide the inputs and multiply the outputs by 255 but that shouldn't be too hard. – Mark Ransom Oct 08 '12 at 21:59
  • Well i was more looking for the math then some lib that can do this. I know routines that do the trick but they all require DWORD or REAL based math. I need something that works on bytes – user613326 Oct 10 '12 at 10:54
1

The easyrgb site has many code snippets for color space conversion. Here's the rgb->hsl code.

David Pointer
  • 905
  • 2
  • 18
  • 31
  • yes but i like to do this using byte math (no DWord based math) – user613326 Oct 10 '12 at 10:52
  • I understand. Are you familiar with scaling? For example, if your floating point numbers have 3 significant digits, multiply the input numbers by 1000, do the integer arithmetic, then integer divide the results by 1000. – David Pointer Oct 10 '12 at 14:12
  • Here's a scaling (fixed point arithmetic) reference starting point: http://en.wikipedia.org/wiki/Fixed-point_arithmetic – David Pointer Oct 10 '12 at 14:33