All files / color invert.js

100% Statements 2/2
100% Branches 0/0
100% Functions 1/1
100% Lines 2/2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                                            3x 3x                  
// @flow
 
import parseToRgb from './parseToRgb'
import toColorString from './toColorString'
 
/**
 * Inverts the red, green and blue values of a color.
 *
 * @example
 * // Styles as object usage
 * const styles = {
 *   background: invert(0.2, '#CCCD64'),
 *   background: invert(0.2, 'rgba(101,100,205,0.7)'),
 * }
 *
 * // styled-components usage
 * const div = styled.div`
 *   background: ${invert(0.2, '#CCCD64')};
 *   background: ${invert(0.2, 'rgba(101,100,205,0.7)')};
 * `
 *
 * // CSS in JS Output
 *
 * element {
 *   background: "#33329b";
 *   background: "rgba(154,155,50,0.7)";
 * }
 */
function invert(color: string): string {
  // parse color string to hsl
  const value = parseToRgb(color)
  return toColorString({
    ...value,
    red: 255 - value.red,
    green: 255 - value.green,
    blue: 255 - value.blue,
  })
}
 
export default invert