Skip to content

Commit d855915

Browse files
committed
Replace JS colour lib with vanilla equivalent.
Fixes workflow editor.
1 parent b962c9b commit d855915

5 files changed

Lines changed: 20 additions & 1854 deletions

File tree

app/assets/javascripts/application.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
//= require bootstrap-tab-history
1818
//= require cytoscape
1919
//= require cytoscape-panzoom
20-
//= require jscolor
2120
//= require jquery.simplecolorpicker.js
2221
//= require split
2322
//= require handlebars.runtime

app/assets/javascripts/workflows.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ var Workflows = {
192192
$('#node-modal-form-title').val(data.name);
193193
$('#node-modal-form-description').val(data.description);
194194
if (data.color) {
195-
$('#node-modal-form-colour')[0].jscolor.fromString(data.color);
195+
$('#node-modal-form-colour').val(data.color);
196196
} else if (data.parent) {
197-
$('#node-modal-form-colour')[0].jscolor.fromString(cy.$('#' + data.parent).data('color'));
197+
$('#node-modal-form-colour').val(cy.$('#' + data.parent).data('color'));
198198
}
199199
$('#node-modal-form-parent-id').val(data.parent);
200200
$('#node-modal-form-x').val(position.x);
@@ -211,7 +211,7 @@ var Workflows = {
211211
description: $('#node-modal-form-description').val(),
212212
html_description: MarkdownIt.render($('#node-modal-form-description').val()),
213213
color: $('#node-modal-form-colour').val(),
214-
font_color: $('#node-modal-form-colour').css("color"),
214+
font_color: Workflows.getTextColor($('#node-modal-form-colour').val()),
215215
parent: $('#node-modal-form-parent-id').val(),
216216
associatedResources: Workflows.associatedResources.fetch(),
217217
ontologyTerms: Workflows.ontologyTerms.fetch()
@@ -470,6 +470,21 @@ var Workflows = {
470470
);
471471
}
472472
}
473+
},
474+
getTextColor: function (hex) {
475+
// Remove the leading '#', if present
476+
hex = hex.replace('#', '');
477+
478+
// Parse r, g, b values
479+
const r = parseInt(hex.substring(0, 2), 16);
480+
const g = parseInt(hex.substring(2, 4), 16);
481+
const b = parseInt(hex.substring(4, 6), 16);
482+
483+
// Calculate perceived brightness (luminance)
484+
const brightness = (0.299 * r) + (0.587 * g) + (0.114 * b);
485+
486+
// Threshold of ~186 is a common practical cutoff
487+
return brightness > 186 ? '#000000' : '#FFFFFF';
473488
}
474489
};
475490

@@ -621,7 +636,6 @@ document.addEventListener("turbolinks:load", function() {
621636
// Initialize
622637
Workflows.cancelState();
623638
Workflows.history.initialize();
624-
jscolor.installByClassName('jscolor');
625639
} else {
626640
// Hiding/revealing of child nodes
627641
if(hideChildNodes) {

app/views/workflows/_node_modal.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<p style="margin: 0px; padding: 5px;">
4343
Click inside the box below to select colour using colour picker:
4444
</p>
45-
<input type="text" class="form-control jscolor {hash:true, zIndex: 100000}" value="#F0721E" id="node-modal-form-colour">
45+
<input type="color" class="form-control" value="#F0721E" id="node-modal-form-colour">
4646
<p style="background-color: #E7E7E7; margin-top: 5px; padding: 5px;">
4747
Or select from pre-defined colours:
4848
<%= render 'workflows/partials/simple_colour_picker' %>

app/views/workflows/partials/_simple_colour_picker.html.erb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
<%= select_tag '', options_for_select(predefined_colours), id: 'simple-colour-picker' %>
2020
<script>
2121
document.addEventListener("turbolinks:load", function() {
22-
$('#simple-colour-picker').simplecolorpicker();
23-
24-
$('#simple-colour-picker').on('change', function (e) {
22+
$('#simple-colour-picker').simplecolorpicker().on('change', function (e) {
2523
var valueSelected = this.value.toUpperCase();
2624
$('#node-modal-form-colour').val(valueSelected);
27-
$('#node-modal-form-colour')[0].jscolor.fromString(valueSelected);
2825
});
2926
});
3027
</script>

0 commit comments

Comments
 (0)