Skip to content

Given an array of converters with a 'from' and 'to' property and an array of available formats in strings, find the smallest number of conversions of the available formats required, in order to reach the requested format

License

Notifications You must be signed in to change notification settings

Maximilianos/solve-conversion-path

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

solve-conversion-path

Install

Via npm:

npm i solve-conversion-path

Usage

Solve-conversion-path was developed for a js color functions library, but it is not restricted to operations on color conversion paths.

This conversion/transform path solver expects to be given three things:

  1. An array of "converters" with "from" and "to" properties. (eg: [{ convert() {}, from: 'hsv', to: 'cmyk' }, ...])
  2. An array of already available "formats". Essentially starting points for the conversion. (eg: ['hex', 'rgb', 'hsl'])
  3. A string representing the desired output "format". (eg: 'cmyk')

If the conversion from the available "formats" to the desired output "format" is possible, the function will return an array with the fewest steps needed to transform one of the already available "formats" into the desired output. (eg: [ { from: 'rgb', to: 'hsv' }, { from: 'hsv', to: 'cmyk' } ])

It is then simple to transform to the desired "format" using Array.prototype.reduce. eg:

const color['cmyk'] = conversionPath.reduce(
	(output, converter) => converter.convert(output),
	color[conversionPath[0].from]
);

Example

import solveConversionPath from 'solve-conversion-path';

const availableFormats = [ 'lab' ];

const converters = [
	{ convert() {}, from: 'rgb', to: 'hex' },
	{ convert() {}, from: 'hex', to: 'rgb' },
	{ convert() {}, from: 'rgb', to: 'hsl' },
	{ convert() {}, from: 'hsl', to: 'rgb' },
	{ convert() {}, from: 'rgb', to: 'lab' },
	{ convert() {}, from: 'lab', to: 'rgb' },
	{ convert() {}, from: 'cmyk', to: 'hex' },
	{ convert() {}, from: 'hex', to: 'cmyk' },
	{ convert() {}, from: 'hsv', to: 'cmyk' },
	{ convert() {}, from: 'cmyk', to: 'hsv' },
];

console.log(
	'solved:',
	JSON.stringify(
		solveConversionPath(converters, availableFormats, 'hsv'),
		null,
		2
	)
);

/* OUTPUTS in console:
solved: [
  {
    "from": "lab",
    "to": "rgb"
  },
  {
    "from": "rgb",
    "to": "hex"
  },
  {
    "from": "hex",
    "to": "cmyk"
  },
  {
    "from": "cmyk",
    "to": "hsv"
  }
]
*/

License

MIT

About

Given an array of converters with a 'from' and 'to' property and an array of available formats in strings, find the smallest number of conversions of the available formats required, in order to reach the requested format

Resources

License

Stars

Watchers

Forks

Packages

No packages published