Skip to content

Commit

Permalink
Merge pull request #108 from MarcInthaamnouay/version/1.2.1
Browse files Browse the repository at this point in the history
Version/1.2.1
  • Loading branch information
shigedangao authored Jan 15, 2019
2 parents d70a752 + b489c80 commit cc8943c
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ matrix:

env:
global:
- VERSION=1.2.0
- VERSION=1.2.1

before_install:
- wget https://github.com/linux-test-project/lcov/releases/download/v1.13/lcov-1.13.tar.gz
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Lymui is a small color conversion library. It's my first project in C !.

## Status

Version: 1.2.0
Version: 1.2.1

## Support OS

Expand Down Expand Up @@ -41,6 +41,10 @@ An exhaustive list of examples are available on the link below.

## Changelogs

* 1.2.1
* * Fix short hand hex which cause some random generated value
* * Add missing support for argb -> xyz

* 1.2.0:
* * Remove free on rgb & xyz for user's reusability
* * Add support for TSL color format
Expand Down
42 changes: 42 additions & 0 deletions docs/argb.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,46 @@ double r = argb->r;
double g = argb->g;
double b = argb->b;
char *error = Argb->error;
```

## Argb to Xyz

Use the method **getXyzFromARgb**. This method return an Xyz struct or NULL.

### Error handling

This method will return NULL when:

- Xyz struct can't be allocated

The method *can return an Xyz struct containing an error property*

- SRgb property not passed

### Parameter

- SRgb pointer struct

### Usage Example

```c
#include "xyz.h"
#include "argb.h"

Argb *argb = malloc(sizeof(SRgb));
argb->r = 1.0;
argb->g = 1.0;
argb->b = 1.0;

// srgb is being freed automatically
Xyz *xyz = getXyzFromARgb(argb);
```

The method will return an Xyz struct which you can access it's property like the example below

```c
double x = xyz->x;
double y = xyz->y;
double z = xyz->z;
char *error = xyz->error;
```
3 changes: 3 additions & 0 deletions docs/hex.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ The method *can return an RGB struct containing an error property*
#include "hex.h"
char *hex = {"050a5f"};
// you can also pass a short hand hex
char *shortHex = {"fff"};
Rgb *rgb = getRGBFromHex(hex);
// RGB will contain R, G, B or an error message
Expand Down
2 changes: 1 addition & 1 deletion docs/srgb.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ char *error = SRgb->error;

## SRgb to Xyz

Use the method **getXyzFromSrgb**. This method return an RGB struct or NULL.
Use the method **getXyzFromSrgb**. This method return an Xyz struct or NULL.

### Error handling

Expand Down
45 changes: 44 additions & 1 deletion lymui/argb.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#include <stdlib.h>
#include "errors.h"
#include "argb.h"
#include "xyz_constant.h"

/**
* @discussion Adobe Gamma Correction adjust the double value to based generated
* @brief Adobe Gamma Correction adjust the double value to based generated
* @param c double
* @return c double
*/
Expand All @@ -26,6 +27,23 @@ static double adobeGammaCorrection(double c) {
return pow(c, 1 / 2.19921875);
}

/**
* @brief Reverse ARgb Gamma
* @param c double
* @return c double
*/
static double reverseARgbGamma(double c) {
if (c <= 0.0) {
return 0.0;
}

if (c >= 1.0) {
return 1.0;
}

return pow(c, 2.19921875);
}

Argb *getARgbFromXyz(Xyz *xyz) {
Argb *argb = malloc(sizeof(Argb));
if (argb == NULL) {
Expand All @@ -48,3 +66,28 @@ Argb *getARgbFromXyz(Xyz *xyz) {

return argb;
}

Xyz *getXyzFromARgb(Argb *argb) {
Xyz *xyz = malloc(sizeof(Xyz));
if (xyz == NULL) {
return NULL;
}

if (argb == NULL) {
xyz->error = NULL_INPUT_PARAM;
return xyz;
}

double _r = reverseARgbGamma(argb->r);
double _g = reverseARgbGamma(argb->g);
double _b = reverseARgbGamma(argb->b);

xyz->x = _r * axr + _g * axg + _b * axb;
xyz->y = _r * ayr + _g * ayg + _b * ayb;
xyz->z = _r * azr + _g * azg + _b * azb;
xyz->error = NULL;

free(argb);

return xyz;
}
12 changes: 7 additions & 5 deletions lymui/hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ char *getHexFromRGB(Rgb *c) {
* @param segment uint8_t
* @return char array
*/
static char* splitSegment(char *hex, uint8_t segment) {
char *part = malloc(sizeof(char) * HEX_GROUP_LEN);
static char *splitSegment(char *hex, uint8_t segment) {
char *part = malloc(sizeof(char *) * HEX_GROUP_LEN);
if (part == NULL) {
return NULL;
}

part[0] = hex[segment - 1];
part[1] = hex[segment];
part[2] = '\0';


return part;
}
Expand All @@ -94,13 +96,13 @@ static uint8_t convertPart(char *part) {
* @param hex char
* @return hex copy
*/
static char* unShorten(char *hex) {
char *copy = malloc(sizeof(6));
static char *unShorten(char *hex) {
char *copy = malloc(sizeof(char) * HEX_SIZE);
if (copy == NULL) {
return NULL;
}

snprintf(copy, sizeof(copy) / sizeof(copy[0]), "%c%c%c%c%c%c",hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]);
snprintf(copy, HEX_SIZE, "%c%c%c%c%c%c",hex[0], hex[0], hex[1], hex[1], hex[2], hex[2]);

return copy;
}
Expand Down
7 changes: 7 additions & 0 deletions lymui/include/argb.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ typedef struct Argb {
*/
Argb *getARgbFromXyz(Xyz *xyz);

/**
* @brief Get Xyz from ARgb
* @param argb ARgb
* @return xyz
*/
Xyz *getXyzFromARgb(Argb *argb);

#endif /* argb_h */
2 changes: 1 addition & 1 deletion lymui/include/hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "rgb.h"

#define HEX_SIZE 7
#define HEX_GROUP_LEN 2
#define HEX_GROUP_LEN 3
#define HEX_BASE 16

/**
Expand Down
53 changes: 53 additions & 0 deletions lymui/tests/argb_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,51 @@ ctest_return_t testMaxARgb(ctest_t *test, void *arg) {
free(rgb);
}

ctest_return_t testArgbToXyz(ctest_t *test, void *arg) {
Argb *argb = malloc(sizeof(Argb));
argb->r = 1.0;
argb->g = 1.0;
argb->b = 1.0;

Xyz *xyz = getXyzFromARgb(argb);

CTAssertDecimalEqual(test, 0.9504, xyz->x, 0.0001, "Expect X to be equal to be equal to %f but got %f", 0.9504, xyz->x);
CTAssertDecimalEqual(test, 1.0000, xyz->y, 0.0001, "Expect Y to be equal to be equal to %f but got %f", 1.0, xyz->y);
CTAssertDecimalEqual(test, 1.0888, xyz->z, 0.0001, "Expect Z to be equal to be equal to %f but got %f", 1.0888, xyz->z);

free(xyz);
}

ctest_return_t testColorArgbToXyz(ctest_t *test, void *arg) {
Argb *argb = malloc(sizeof(Argb));
argb->r = 0.196089;
argb->g = 0.039087;
argb->b = 0.372496;

Xyz *xyz = getXyzFromARgb(argb);

CTAssertDecimalEqual(test, 0.0376, xyz->x, 0.0001, "Expect X to be equal to be equal to %f but got %f", 0.0375, xyz->x);
CTAssertDecimalEqual(test, 0.0173, xyz->y, 0.0001, "Expect Y to be equal to be equal to %f but got %f", 0.0173, xyz->y);
CTAssertDecimalEqual(test, 0.1137, xyz->z, 0.0001, "Expect Z to be equal to be equal to %f but got %f", 0.1137, xyz->z);

free(xyz);
}

ctest_return_t testDarkArgbToXyz(ctest_t *test, void *arg) {
Argb *argb = malloc(sizeof(Argb));
argb->r = 0.0;
argb->g = 0.0;
argb->b = 0.0;

Xyz *xyz = getXyzFromARgb(argb);

CTAssertDecimalEqual(test, 0.0, xyz->x, 0.01, "Expect X to be equal to be equal to %f but got %f", 0.0, xyz->x);
CTAssertDecimalEqual(test, 0.0, xyz->y, 0.01, "Expect Y to be equal to be equal to %f but got %f", 0.0, xyz->y);
CTAssertDecimalEqual(test, 0.0, xyz->z, 0.01, "Expect Z to be equal to be equal to %f but got %f", 0.0, xyz->z);

free(xyz);
}

ctest_return_t testNullARgb(ctest_t *test, void *arg) {
Argb *argb = getARgbFromXyz(NULL);
CTAssertStringEqual(test, argb->error, NULL_INPUT_STRUCT, "Expect Error to be equal to %s", NULL_INPUT_STRUCT);
Expand All @@ -85,10 +130,18 @@ ctcase_t *wrapARgbCreationTest() {
ctest_t *testEmptyARgb = ctest("Creation of an Adobe RGB from Rgb black color", testARgbEmpty, NULL);
ctest_t *testARgbMax = ctest("Creation of an Adobe RGB from Rgb with with white color", testMaxARgb, NULL);

// Argb to XYZ
ctest_t *testWhiteArgbToXyz = ctest("Creation of a white XYZ from an Adobe RGB", testArgbToXyz, NULL);
ctest_t *testOctobArgbToXyz = ctest("Creation of a color XYZ from an Adobe RGB", testColorArgbToXyz, NULL);
ctest_t *testBlackArgbToXyz = ctest("Creation of a black XYZ from an Adobe RGB", testDarkArgbToXyz, NULL);

ctctestadd(ARgbCase, testARgb);
ctctestadd(ARgbCase, testARgbNull);
ctctestadd(ARgbCase, testEmptyARgb);
ctctestadd(ARgbCase, testARgbMax);
ctctestadd(ARgbCase, testWhiteArgbToXyz);
ctctestadd(ARgbCase, testOctobArgbToXyz);
ctctestadd(ARgbCase, testBlackArgbToXyz);

return ARgbCase;
}

0 comments on commit cc8943c

Please sign in to comment.