Skip to content

Commit

Permalink
feat: Colour Accessibility restriction #56 #45 #25
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomrebelo committed Aug 11, 2023
1 parent 3c52f05 commit 4560a34
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 75 deletions.
9 changes: 8 additions & 1 deletion evo-poster.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ const TYPEFACES = {
}
}

const COLOR = {
MIN_CONTRAST: 2.5,
MAX_COLOR_SCHEME_ATTEMPT: 200,
}


export default {
typography: TYPEFACES !== undefined ? TYPEFACES : {}
typography: TYPEFACES !== undefined ? TYPEFACES : {},
color: COLOR !== undefined ? COLOR : {}
}
41 changes: 41 additions & 0 deletions src/client/controllers/ColorGenerator.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import {harmony, analogue, complement, triad } from 'simpler-color';
import * as config from '../../../evo-poster.config.js';

const MIN_CONTRAST = config["default"].color !== null ? config["default"].color.MIN_CONTRAST : 10;

export const randomScheme = () => {
const baseColour = randomColour();
return complementAndAnalogueScheme(baseColour);
}

export const contrastChecker = (baseColor, colorA, colorB) => {
const baseColorLuminance = luminance(baseColor);
const colorALuminance = luminance(colorA);
const colorBLuminance = luminance(colorB);
const min = Math.min(contrastRatio(baseColorLuminance, colorALuminance), contrastRatio(baseColorLuminance, colorBLuminance));
return min > MIN_CONTRAST;
}

export const complementAndAnalogueScheme = (baseColour) => {
const colorA = complement(baseColour, 1);

const colorB = analogue(colorA, Math.round(-2+(Math.random()*4)));
return {
baseColour: baseColour,
Expand Down Expand Up @@ -55,4 +67,33 @@ export const randomColour = () => {
}



const hexToRGB = (hex) => {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}

const luminance = (c) => {
c = hexToRGB(c);
let [lumR, lumG, lumB] = Object.keys(c).map(key => {
let proportion = c[key] / 255;
return proportion <= 0.03928
? proportion / 12.92
: Math.pow((proportion + 0.055) / 1.055, 2.4);
});
return 0.2126 * lumR + 0.7152 * lumG + 0.0722 * lumB;
}

const contrastRatio = (luminance1, luminance2) => {
let lighterLum = Math.max(luminance1, luminance2);
let darkerLum = Math.min(luminance1, luminance2);

return (lighterLum + 0.05) / (darkerLum + 0.05);
}


export default randomScheme;
16 changes: 14 additions & 2 deletions src/client/controllers/Population.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {Params} from "../Params.js";
import Poster, {Grid} from "./Poster.js";
import {randomScheme} from "./ColorGenerator.js";
import {contrastChecker, randomScheme} from "./ColorGenerator.js";
import {shuffleArr, sumArr, sus, swap} from "../utils.js";

import * as config from './../../../evo-poster.config.js';


const SIZE_MUTATION_ADJUST = 5;
const TOURNAMENT_SIZE = 10;
const MAX_COLOR_SCHEME_ATTEMPT = config["default"]["COLOR"] !== undefined ? config["default"]["COLOR"]["MAX_COLOR_SCHEME_ATTEMPT"] : 200;

export class Population {
#typefaces;
Expand Down Expand Up @@ -196,7 +200,15 @@ export class Population {

// colours scheme
if (Math.random() < prob) {
const colorScheme = randomScheme();
let colorContrast = false;
let colorScheme;
let colorAttempt = 0;
while (!colorContrast || colorAttempt > MAX_COLOR_SCHEME_ATTEMPT) {
colorScheme = randomScheme();
colorContrast = contrastChecker(colorScheme["baseColour"], colorScheme["colorA"], colorScheme["colorB"]);
colorAttempt++;
}

// mutate background colours
if (!this.params["background"]["lock"][1]) {
ind.genotype["background"]["colors"][0] = colorScheme.colorA;
Expand Down
17 changes: 14 additions & 3 deletions src/client/controllers/Poster.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import {Params} from "../Params.js";
import backgroundStyles from "./BackgroundStyles.js";

import * as evaluator from "../../@evoposter/evaluator/src/index.mjs";
import {randomScheme} from "./ColorGenerator.js";
import {randomScheme, contrastChecker} from "./ColorGenerator.js";
import {sumArr} from "../utils.js";
import {alignment, semanticsEmphasis, whiteSpaceFraction} from "../../@evoposter/evaluator/src/index.mjs";

import * as config from './../../../evo-poster.config.js';
const MAX_COLOR_SCHEME_ATTEMPT = config["default"]["COLOR"] !== undefined ? config["default"]["COLOR"]["MAX_COLOR_SCHEME_ATTEMPT"] : 200;


class Poster {
#showGrid = false;
Expand Down Expand Up @@ -79,8 +82,16 @@ class Poster {
return new Poster(this.n, this.generation, this.params, genotypeCopy);
}

#generateGenotype = (params) => {
const colorScheme = randomScheme();
#generateGenotype = (params, ) => {
// generate scheme
let colorContrast = false;
let colorScheme;
let colorAttempt = 0;
while (!colorContrast || colorAttempt > MAX_COLOR_SCHEME_ATTEMPT) {
colorScheme = randomScheme();
colorContrast = contrastChecker(colorScheme["baseColour"], colorScheme["colorA"], colorScheme["colorB"]);
colorAttempt++;
}

// define grid
const grid = new Grid(
Expand Down
Loading

0 comments on commit 4560a34

Please sign in to comment.