21 Commits

Author SHA1 Message Date
David Briscoe
7d99a08134 [Breaking] color: Convert hue [0,359] -> [0,1]
imgui [uses 0-1 for everything internally](
https://github.com/ocornut/imgui/blob/master/imgui.cpp#L2045).

[Both Unity](https://docs.unity3d.com/ScriptReference/Color.RGBToHSV.html)
[and Godot](https://docs.godotengine.org/en/stable/classes/class_color.html#class-color-property-h)
use H, S, and V in the range 0.0 to 1.0

Since we're switching colours out of 0,255 we should also switch hue out
of 0,359. Now all of our r,g,b,a,h,s,v values are all in 0,1.
2022-04-01 21:55:40 -07:00
David Briscoe
0c12f17805 color: Linear alpha in linear<->gamma conversions
Maintain linear alpha when converting to gamma.

Update to latest wikipedia links.

Add simple test that does the round trip conversion.
2022-04-01 21:55:40 -07:00
David Briscoe
aec3b9fd7f Prevent nan when using color_to_hsv(white)
When r,g,b are equal, delta will be zero and hue will be nan due to
divide by zero.

This is also an issue with the reference C++ code:
https://tio.run/##Sy4o0E3OScxL//9fOTMvOac0JVXBJjO/uKQoNTHXjiszr0QhNzEzT0NToZqLs7gkxcoqOb@0RMHGRkHJIzUnJ19HITy/KCdFUQkkBJZPzUvJseaq/f8fAA

An alternative approach is to always add epsilon or use a different hue
calculation:
    http://web.archive.org/web/20210515082154/http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
2022-04-01 21:55:32 -07:00
David Briscoe
41b1f0b5b9 color: Add more tests 2022-04-01 21:47:56 -07:00
David Briscoe
343f320e2f [Breaking] color: Convert 0-255 -> 0-1 range
Our hsv logic assumes 0-1 (cs.rit.edu says "r,g,b values are from 0 to
1") and it makes more sense to provide __mul for multiplying in 0-1
since the result will stay in that range. Additionally, love switched to
0-1 color since 11.0.

Included an as_255() function to unpack the color in 0-255 for some
amount of backwards compatibility. Doesn't make sense to provide any
kind of color object for it since most of our functions won't work
correctly.

Add tests for hue(), saturation(), value() that fail (even with /255
removed) for the old code, but pass on the new 0-1 range because the hsv
logic outputs colors in 0-1. Test comparisons use reduced precision
because my input data has limited precision.
2022-04-01 21:47:56 -07:00
David Briscoe
df92f0c5cd [Breaking] color: Make expected range match docs
lighten/darken: documented range is 0-255, so we don't need to multiply.
Makes more sense to work with colors in the same range that we're
incrementing by, but this changes behaviour.

saturation/value: we use a percent, so it's 0-1. It's clamped so that's
obviously expected.

Add tests to validate this new behaviour.
2022-04-01 21:47:09 -07:00
David Briscoe
509594aec2 color: Use color_mt and normal indexing
Fix #34.

Set color_mt on new colors so add, subtract, multiply, is_color work.

We don't use ffi for color (32cf0e8), so remove cdata check for 0-based
offset indexing.

Add tests for creating colours and using operators.
2022-04-01 19:55:55 -07:00
David Briscoe
c26cc1a508 Include offending type in precondition failure msg
Output the type that was incorrect to make it more obvious what the user
is doing wrong without them adding logging.

To avoid string building during lots of vector math, introduced a
private precond module that only builds the strings when an error
occurs. It uses error() to put the resulting error message at the caller
to cpml.

Before:

  lua: ~/cpml/modules/vec2.lua:52: new: Wrong argument type for x (<number> expected)
  lua: ~/cpml/modules/vec2.lua:424: __add: Wrong argument type for right hand operand. (<cpml.vec2> expected)

  example stack traceback:
  	[C]: in function 'assert'
  	~/cpml/modules/vec2.lua:424: in metamethod '__add'
  	test_cpml.lua:32: in main chunk
  	[C]: in ?

After:

  lua: test_cpml.lua:31: new: Wrong argument type for x: string (<number> expected)
  lua: test_cpml.lua:32: __add: Wrong argument type 'string' for right hand operand. (<cpml.vec2> expected)

  example stack traceback:
  	[C]: in function 'error'
  	~/cpml/modules/_private_precond.lua:13: in function 'modules._private_precond.assert'
  	~/cpml/modules/vec2.lua:425: in metamethod '__add'
  	test_cpml.lua:32: in main chunk
  	[C]: in ?

The tracebacks are longer, but the initial error is at the location of
the mistake and the output includes the input type.
2022-03-27 09:56:15 -07:00
mcc
bb4d945c94 Various cleanup on color class
- Expose table versions of color-to-hsv and hsv-to-color
- Proper behavior on color.new(nil)
- Documentation
2018-01-26 10:00:38 -05:00
karai17
e3cafd6c2f tidied up color 2016-12-14 20:45:11 -04:00
Colby Klein
32cf0e8608 Remove FFI from color. It's broken. 2016-08-13 09:36:25 -07:00
karai17
6daa845533 No unnecessary lines! 2016-07-24 11:48:58 -03:00
karai17
3c3eebd66f Minor tweak 2016-07-24 11:39:36 -03:00
karai17
375f748c1f FFI'd color 2016-07-23 23:16:10 -03:00
karai17
d34cca315f Removed ffi from mat4, it caused a lot of problems and didn't work. 2016-07-22 17:13:11 -03:00
karai17
5f7a7f29fa Added quaternion tests
* Fixed an issue with is_quat, is_vec3, is_vec2, and is_mat4 methods
* Fixed a minor issue with local variables being declared too late
2016-07-21 20:04:32 -03:00
karai17
2950352ac8 Updated color module, etc. 2016-07-20 22:17:25 -03:00
Colby Klein
78989682cf Add LDoc documentation. Utils done. 2015-09-11 07:59:05 -07:00
Colby Klein
c102ded4ba Add operators and lerp to cpml.color 2015-07-25 07:17:50 -07:00
Colby Klein
cb4ba40bad Add gamma correction to color module (for hate) 2015-03-22 16:54:56 -07:00
Colby Klein
5a1074466b Add color and utils modules.
A pile of color functions needed for DOMinatrix.
2015-03-16 08:24:11 -07:00