Skip to content

Commit

Permalink
test: add tests and restrict input
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaello committed Aug 26, 2023
1 parent 51a474b commit fc512d4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
36 changes: 36 additions & 0 deletions __tests__/Tinycolor_tests.res
Original file line number Diff line number Diff line change
Expand Up @@ -600,4 +600,40 @@ describe("conversion utils", () => {

expect(hsl.s) |> toEqual(0.5)
})
test("hslToRgb()", () => {
let rgb = TinyColor.hslToRgb(360, 100, 50)
let expectedRgb: TinyColor.rgb = {r: 255, g: 0, b: 0}

expect(rgb) |> toEqual(expectedRgb)
})
test("rgbToHsv()", () => {
let hsv = TinyColor.rgbToHsv(10, 20, 255)

expect(hsv.v) |> toEqual(1.0)
})
test("hsvToRgb()", () => {
let rgb = TinyColor.hsvToRgb(360, 100, 100)
let expectedRgb: TinyColor.rgb = {r: 255, g: 0, b: 0}
expect(rgb) |> toEqual(expectedRgb)
})
test("rgbToHex() works with 3char", () => {
let hex = TinyColor.rgbToHex(~allow3Char=true, 34, 68, 136)
let expectedHex = "248"
expect(hex) |> toEqual(expectedHex)
})
test("rgbToHex()", () => {
let hex = TinyColor.rgbToHex(34, 68, 136)
let expectedHex = "224488"
expect(hex) |> toEqual(expectedHex)
})
test("rgbaToHex()", () => {
let hex = TinyColor.rgbaToHex(34, 68, 136, 0.5)
let expectedHex = "22448880"
expect(hex) |> toEqual(expectedHex)
})
test("rgbaToArgbHex()", () => {
let hex = TinyColor.rgbaToArgbHex(34, 68, 136, 0.5)
let expectedHex = "80224488"
expect(hex) |> toEqual(expectedHex)
})
})
26 changes: 13 additions & 13 deletions src/TinyColor.res
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,18 @@ external rgbToRgb: (int, int, int) => rgb = "rgbToRgb"
/**
Handle bounds / percentage checking to conform to CSS color spec <http://www.w3.org/TR/css3-color/>
*Assumes:* r, g, b in [0, 255] or [0, 1]
*Assumes:* r, g, b in [0, 255]
*Returns:* { r, g, b } in [0, 255]
*/
let rgbToRgb = (r: int, g: int, b: int) => rgbToRgb(r, g, b)

@module("@ctrl/tinycolor")
external rgbToHsl: (int, int, int) => hsl = "rgbToHsl"
external rgbToHsl: (int, int, int) => hslRatio = "rgbToHsl"
/**
Converts an RGB color value to HSL.
*Assumes:* r, g, and b are contained in [0, 255] or [0, 1]
*Assumes:* r, g, and b are contained in [0, 255]
*Returns:* { h, s, l } in [0,1]
*/
Expand All @@ -419,18 +419,18 @@ external hslToRgb: (int, int, int) => rgb = "hslToRgb"
/**
Converts an HSL color value to RGB.
*Assumes:* h is contained in [0, 1] or [0, 360] and s and l are contained [0, 1] or [0, 100]
*Assumes:* h is contained in [0, 360] and s and l are contained [0, 100]
*Returns:* { r, g, b } in the set [0, 255]
*/
let hslToRgb = (h: int, s: int, l: int) => hslToRgb(h, s, l)

@module("@ctrl/tinycolor")
external rgbToHsv: (int, int, int) => hsv = "rgbToHsv"
external rgbToHsv: (int, int, int) => hsvRatio = "rgbToHsv"
/**
Converts an RGB color value to HSV
*Assumes:* r, g, and b are contained in the set [0, 255] or [0, 1]
*Assumes:* r, g, and b are contained in the set [0, 255]
*Returns:* { h, s, v } in [0,1]
*/
Expand All @@ -441,7 +441,7 @@ external hsvToRgb: (int, int, int) => rgb = "hsvToRgb"
/**
Converts an HSV color value to RGB.
*Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100]
*Assumes:* h is contained in [0, 360] and s and v are contained in [0, 100]
*Returns:* { r, g, b } in the set [0, 255]
*/
Expand All @@ -459,19 +459,19 @@ external rgbToHex: (int, int, int, bool) => string = "rgbToHex"
let rgbToHex = (~allow3Char: bool=false, r: int, g: int, b: int) => rgbToHex(r, g, b, allow3Char)

@module("@ctrl/tinycolor")
external rgbaToHex: (int, int, int, int, bool) => string = "rgbaToHex"
external rgbaToHex: (int, int, int, float, bool) => string = "rgbaToHex"
/**
Converts an RGBA color plus alpha transparency to hex
*Assumes:* r, g, b are contained in the set [0, 255] and a in [0, 1].
*Returns:* a 4 or 8 character rgba hex
*/
let rgbaToHex = (~allow4Char: bool=false, r: int, g: int, b: int, a: int) =>
let rgbaToHex = (~allow4Char: bool=false, r: int, g: int, b: int, a: float) =>
rgbaToHex(r, g, b, a, allow4Char)

@module("@ctrl/tinycolor")
external rgbaToArgbHex: (int, int, int, int) => string = "rgbaToArgbHex"
external rgbaToArgbHex: (int, int, int, float) => string = "rgbaToArgbHex"
/**
Converts an RGBA color to an ARGB Hex8 string
Rarely used, but required for "toFilter()"
Expand All @@ -480,15 +480,15 @@ external rgbaToArgbHex: (int, int, int, int) => string = "rgbaToArgbHex"
*Returns:* a 8 character argb hex
*/
let rgbaToArgbHex = (r: int, g: int, b: int, a: int) => rgbaToArgbHex(r, g, b, a)
let rgbaToArgbHex = (r: int, g: int, b: int, a: float) => rgbaToArgbHex(r, g, b, a)

/** Converts a decimal to a hex value */
@module("@ctrl/tinycolor")
external convertDecimalToHex: int => string = "convertDecimalToHex"
external convertDecimalToHex: float => string = "convertDecimalToHex"

/** Converts a hex value to a decimal */
@module("@ctrl/tinycolor")
external convertHexToDecimal: string => int = "convertHexToDecimal"
external convertHexToDecimal: string => float = "convertHexToDecimal"

/** Parse a base-16 hex value into a base-10 integer */
@module("@ctrl/tinycolor")
Expand Down

0 comments on commit fc512d4

Please sign in to comment.