Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex <=> Hsv mutual conversion not equal #6

Closed
noyobo opened this issue Jun 21, 2015 · 2 comments
Closed

Hex <=> Hsv mutual conversion not equal #6

noyobo opened this issue Jun 21, 2015 · 2 comments

Comments

@noyobo
Copy link

noyobo commented Jun 21, 2015

Thank you for your the colr 😃

I found some colors can not be equal when converted;

test.js

'use strict';

var assert = require('assert');
var Colr = require('../index');

var colr = new Colr();

describe('clor hsv', function() {
  // #81468f
  it('from hex #81468f should to hsv [289, 51, 56]', function(done) {
    var hsv = colr.fromHex('#81468f').toHsvArray();
    assert.deepEqual(hsv, [289, 51, 56]);
    done();
  });

  it('from hsv [289, 51, 56] should to hex #81468f', function(done) {
    var hex = colr.fromHsvArray([289, 51, 56]).toHex();
    assert.equal(hex, '#81468f');
    done();
  });

  it('from hsv [288, 51, 56] should to hex #80468f', function(done) {
    var hex = colr.fromHsvArray([288, 51, 56]).toHex();
    assert.equal(hex, '#80468f');
    done();
  });

  it('from hex #80468f should to hsv [288, 51, 56]', function(done) {
    var hsv = colr.fromHex('#80468f').toHsvArray();
    assert.deepEqual(hsv, [289, 51, 56]);
    done();
  });
});

result:

  clor hsv
    ✓ from hex #81468f should to hsv [289, 51, 56]
    1) from hsv [289, 51, 56] should to hex #81468f
    ✓ from hsv [288, 51, 56] should to hex #80468f
    2) from hex #80468f should to hsv [288, 51, 56]


  2 passing (23ms)
  2 failing
noyobo added a commit to noyobo/color-picker that referenced this issue Jun 21, 2015
@stayradiated
Copy link
Owner

There seems to be a mistake in the last test:

it('from hex #80468f should to hsv [288, 51, 56]', function(done) {
    ...
    assert.deepEqual(hsv, [289, 51, 56]);
    ...
});

After fixing that, the results I get when I run the test are:

  clor hsv
    1) from hex #81468f should to hsv [289, 51, 56]
    ✓ from hsv [289, 51, 56] should to hex #81468f
    ✓ from hsv [288, 51, 56] should to hex #80468f
    ✓ from hex #80468f should to hsv [288, 51, 56]

  3 passing (18ms)
  1 failing

I then checked in Photoshop, using the color picker:

// hex to hsv
#80468f -> hsv(288, 51, 56)
#81468f -> hsv(288, 51, 56)

// hsv to hex
hsv(288, 51, 56) -> #80468f
hsv(289, 51, 56) -> #81468f

I think this is just a rounding error due to not enough significant figures.

If you need accurate results, try using:

  • .toRawHsvArray() instead of .toHsvArray()
  • .toRawRgbArray() instead of .toHex()
Conversions
// hex to rgb to raw hsv to rounded hsv
#80468f -> rgb(128, 70, 143) -> hsv(287.67, 51.05, 56.08) -> hsv(288, 51, 56)
#81468f -> rgb(129, 70, 143) -> hsv(288.49, 51.05, 56.08) -> hsv(288, 51, 56)

// rounded hsv to raw rgb to rounded rgb to hex
hsv(288, 51, 56) -> rgb(128.23, 69.97, 142.8) -> rgb(128, 70, 143) -> #80468f 
hsv(289, 51, 56) -> rgb(129.45, 69.97, 142.8) -> rgb(129, 70, 143) -> #81468f

// raw rgb to raw hsv
rgb(128.23, 69.97, 142.8) -> hsv(288, 51, 56)
rgb(129.45, 69.97, 142.8) -> hsv(289, 51, 56)

// raw hsv to raw rgb
hsv(287.67, 51.05, 56.08) -> rgb(128, 70, 143)
hsv(288.49, 51.05, 56.08) -> rgb(129, 70, 143)
Extended Tests
'use strict';

var assert = require('assert');
var Colr = require('./index');

var colr = new Colr();

describe('colr hsv', function() {

  // rgb[128, 70, 143] = #80468f
  // rgb[129, 70, 143] = #81468f

  it('from rgb [128, 70, 143] to hsv [288, 51, 56]', function(done) {
    var c = colr.fromRgbArray([128, 70, 143]);
    assert.deepEqual(c.toRawHsvArray(), [
      287.6712328767123,
      51.048951048951054,
      56.07843137254902,
    ]);
    assert.deepEqual(c.toHsvArray(), [
      288,
      51,
      56,
    ]);
    done();
  });

  it('from rgb [129, 70, 143] to hsv [288, 51, 56]', function(done) {
    var c = colr.fromRgbArray([129, 70, 143]);
    assert.deepEqual(c.toRawHsvArray(), [
      288.4931506849315,
      51.048951048951054,
      56.07843137254902,
    ]);
    assert.deepEqual(c.toHsvArray(), [
      288,
      51,
      56,
    ]);
    done();
  });

  it('from hsv [288, 51, 56] to rgb [128, 70, 143]', function(done) {
    var c = colr.fromHsvArray([288, 51, 56]);
    assert.deepEqual(c.toRawRgbArray(), [
      128.2344,
      69.97200000000001,
      142.8,
    ]);
    assert.deepEqual(c.toRgbArray(), [
      128,
      70,
      143,
    ]);
    done();
  });

  it('from hsv [289, 51, 56] to rgb [129, 70, 143]', function(done) {
    var c = colr.fromHsvArray([289, 51, 56]);
    assert.deepEqual(c.toRawRgbArray(), [
      129.44819999999999,
      69.97200000000001,
      142.8,
    ]);
    assert.deepEqual(c.toRgbArray(), [
      129,
      70,
      143,
    ]);
    done();
  });
});
Test Results
  colr hsv
    ✓ from rgb [128, 70, 143] to hsv [288, 51, 56]
    ✓ from rgb [129, 70, 143] to hsv [288, 51, 56]
    ✓ from hsv [288, 51, 56] to rgb [128, 70, 143]
    ✓ from hsv [289, 51, 56] to rgb [129, 70, 143]

@noyobo
Copy link
Author

noyobo commented Jul 1, 2015

Sorry for mistake in the last test 😧

Thank you for your answer, This seems a reasonable a rounding value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants