Skip to content

Commit

Permalink
Color/Ensor - Do some fixes and changes to produce better results
Browse files Browse the repository at this point in the history
  • Loading branch information
lainsce committed Jun 30, 2024
1 parent 01ef792 commit ecbe670
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 65 deletions.
2 changes: 1 addition & 1 deletion lib/Utils/Color.vala
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ namespace He.Color {
0.7490 * 255
};

private const double LSTAR = 49.6;
private const double LSTAR = 50.0;
}
67 changes: 35 additions & 32 deletions lib/Utils/Color/HCT/HCT.vala
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ namespace He.Color {
return result;
}

// Disliked means a yellow-green that's not neutral
public static bool disliked (He.Color.HCTColor hct) {
bool hue_passes = Math.round (hct.h) >= 90.0 && Math.round (hct.h) <= 111.0;
bool chroma_passes = Math.round (hct.c) > 16.0;
bool tone_passes = Math.round (hct.t) < 70.0;
bool tone_passes = Math.round (hct.t) < 65.0;

return hue_passes && chroma_passes && tone_passes;
}

/** If color is disliked, lighten it to make it likable. */
// If color is disliked, lighten it to make it likable.
public static He.Color.HCTColor fix_disliked (He.Color.HCTColor hct) {
if (disliked (hct)) {
return He.Color.from_params (hct.h, hct.c, 70.0);
Expand All @@ -31,51 +32,53 @@ namespace He.Color {
public string hct_to_hex (double hue, double chroma, double lstar) {
He.Color.HCTColor hct = {hue, chroma, lstar};

// If color is mono
// If color is mono
if (hct.c < 1.0001 || hct.t < 0.0001 || hct.t > 99.9999) {
return hexcode_argb (He.MathUtils.argb_from_lstar (hct.t));
// Else…
} else {
hue = He.MathUtils.sanitize_degrees (hct.h);
double hr = hue / 180 * Math.PI;
double y = He.MathUtils.y_from_lstar (hct.t);
int exact_answer = find_result_by_j (hr, hct.c, y);

if (exact_answer != 0) {
return hexcode_argb (exact_answer);
}

double[] linrgb = He.MathUtils.bisect_to_limit (y, hr);
return hexcode_argb (argb_from_linrgb (linrgb));
}

// Else...
hue = He.MathUtils.sanitize_degrees (hct.h);
double hr = hue / 180 * Math.PI;
double y = He.MathUtils.y_from_lstar (hct.t);
int exact_answer = find_result_by_j (hr, hct.c, y);

if (exact_answer != 0) {
return hexcode_argb (exact_answer);
}

double[] linrgb = He.MathUtils.bisect_to_limit (y, hr);

return hexcode_argb (argb_from_linrgb (linrgb));
}

public int hct_to_argb (double hue, double chroma, double lstar) {
// If color is mono
// If color is mono
if (chroma < 1.0001 || lstar < 0.0001 || lstar > 99.9999) {
return He.MathUtils.argb_from_lstar (lstar);
}

// Else...
double hues = He.MathUtils.sanitize_degrees (hue);
double hr = hues / 180 * Math.PI;
double y = He.MathUtils.y_from_lstar (lstar);
int exact_answer = find_result_by_j (hr, chroma, y);
// Else…
} else {
double hues = He.MathUtils.sanitize_degrees (hue);
double hr = hues / 180 * Math.PI;
double y = He.MathUtils.y_from_lstar (lstar);
int exact_answer = find_result_by_j (hr, chroma, y);

if (exact_answer != 0) {
return exact_answer;
}
if (exact_answer != 0) {
return exact_answer;
}

double[] linrgb = He.MathUtils.bisect_to_limit (y, hr);
double[] linrgb = He.MathUtils.bisect_to_limit (y, hr);

return argb_from_linrgb (linrgb);
return argb_from_linrgb (linrgb);
}
}

public HCTColor hct_blend (HCTColor a, HCTColor b) {
var difference_degrees = He.MathUtils.difference_degrees (a.h, b.h);
var rot_deg = MathUtils.min (difference_degrees / 2, 15.0);
var output = He.MathUtils.sanitize_degrees (a.h + (rot_deg * He.MathUtils.rotate_direction (a.h, b.h)));
var rot_deg = MathUtils.min (difference_degrees * 0.5, 15.0);
var output =
He.MathUtils.sanitize_degrees (
a.h
+ rot_deg * He.MathUtils.rotate_direction (a.h, b.h));

return fix_disliked ({output, a.c, a.t});
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Utils/Color/XYZ.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace He.Color {
}

private const double[,] CAM16RGB_TO_XYZ = {
{1.862067800, -1.01125470, 0.14918678},
{0.387526540, 0.62144744, -0.00897398},
{-0.01584150, -0.03412294, 1.04996440}
{1.8620678, -1.0112547, 0.14918678},
{0.38752654, 0.62144744, -0.00897398},
{-0.01584150, -0.03412294, 1.0499644}
};

private const double[,] RGB_TO_XYZ = {
Expand Down
6 changes: 0 additions & 6 deletions lib/Utils/Ensor/Quantize/QuantizerWsmeans.vala
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ public class He.QuantizerWsmeans : Object {
var distance_to_index_matrix = create_2d_list (cluster_count, cluster_count, (i) => new DistanceToIndex ());

for (int iteration = 0; iteration < MAX_ITERATIONS; iteration++) {
print ("starting iteration %d\n", iteration);

// Calculate cluster distances
for (int i = 0; i < cluster_count; i++) {
for (int j = i + 1; j < cluster_count; j++) {
Expand Down Expand Up @@ -200,12 +198,8 @@ public class He.QuantizerWsmeans : Object {
double c = component_c_sums[i] / count;
clusters.insert_val (i, {a, b, c});
}

print ("finished iteration %u\n", iteration);
}

print ("checkpoint neko\n");

var swatches = new GLib.Array<Swatch> ();
var cluster_argbs = new GLib.Array<int?> ();

Expand Down
25 changes: 6 additions & 19 deletions lib/Utils/Ensor/Score.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
namespace He {
public class Score {

const double CUTOFF_CHROMA = 15.0;
const double CUTOFF_EXCITED_PROPORTION = 0.01;
const double CUTOFF_TONE = 10.0;
const double TARGET_CHROMA = 48.0;
const double WEIGHT_PROPORTION = 0.7;
const double WEIGHT_CHROMA_ABOVE = 0.3;
const double WEIGHT_CHROMA_BELOW = 0.1;
const double WEIGHT_PROPORTION = 0.7;
const double CUTOFF_CHROMA = 5.0;
const double CUTOFF_EXCITED_PROPORTION = 0.01;
const string TAU_PURPLE = "#FF8C56BF";

public class AnnotatedColor {
public int argb;
Expand Down Expand Up @@ -54,8 +54,6 @@ namespace He {
int hue = (int)He.MathUtils.sanitize_degrees (Math.round (cam.h));
hue_proportions[hue] += proportion;

//print ("HUE PROPORTIONS FOR HUE %d: %f\n", hue, hue_proportions[sanitized_hue]);

colors.add (new AnnotatedColor () {
argb = argbs[i],
cam_hue = cam.h,
Expand All @@ -67,7 +65,7 @@ namespace He {

for (int i = 0; i < input_size; i++) {
int hue = (int)Math.round (colors.get (i).cam_hue);
for (int j = (hue - 15); j < (hue + 15); j++) {
for (int j = (hue - 14); j < (hue + 16); j++) {
int sanitized_hue = (int)He.MathUtils.sanitize_degrees (j);
colors.get (i).excited_proportion += hue_proportions[sanitized_hue];
}
Expand All @@ -82,13 +80,7 @@ namespace He {
colors.get (i).score = chroma_score + proportion_score;
}

for (int i = 0; i < input_size; i++) {
print ("COLORS #%d BEFORE: %s SCORE: %f\n", i, Color.hexcode_argb (colors.get (i).argb), colors.get (i).score);
}
colors.sort (AnnotatedColor.cmp);
for (int i = 0; i < input_size; i++) {
print ("COLORS #%d AFTER: %s SCORE: %f\n", i, Color.hexcode_argb (colors.get (i).argb), colors.get (i).score);
}

GLib.GenericArray<AnnotatedColor> selected_colors = new GLib.GenericArray<AnnotatedColor> ();
for (int i = 0; i < input_size; i++) {
Expand All @@ -111,13 +103,9 @@ namespace He {
selected_colors.add (colors.get (i));
}

for (int i = 0; i < selected_colors.length; i++) {
print ("COLORS #%d AFTER SELECTION: %s\n", i, Color.hexcode_argb (selected_colors.get (i).argb));
}

if (selected_colors.length == 0) {
selected_colors.add (new AnnotatedColor () {
argb = int.parse ("#FF8C56BF"),
argb = int.parse (TAU_PURPLE),
cam_hue = 311.12,
cam_chroma = 57.36,
excited_proportion = 0,
Expand All @@ -138,7 +126,6 @@ namespace He {

bool good_color_finder (AnnotatedColor color) {
return color.cam_chroma >= CUTOFF_CHROMA &&
He.MathUtils.lstar_from_argb (color.argb) >= CUTOFF_TONE &&
color.excited_proportion >= CUTOFF_EXCITED_PROPORTION;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Utils/ViewingConditions.vala
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public class He.ViewingConditions : Object {
}

public static ViewingConditions make (
double[] white_point = {95.04705586542832, 100.00000000000000, 108.8828736395884},
double adapting_luminance = -1,
double bg_lstar = 49.6,
double surround = 0.69,
double[] white_point = {95.047, 100.0, 108.883},
double adapting_luminance = (200.0 / Math.PI * He.MathUtils.y_from_lstar (50.0) / 100.0),
double bg_lstar = 50.0,
double surround = 2.0,
bool discount_illuminant = false
) {
bg_lstar = MathUtils.max (0.1, bg_lstar);
Expand Down

0 comments on commit ecbe670

Please sign in to comment.