<commit_before>// main.js
// The mains.

// Selected bands.
var sel_bands = {
	values: [ "black", "black", "black" ],
	tolerance: "gold"
};

/**
 * Just a simple onLoad event handler.
 */
function onload() {
	var list = [ resistance.values.red.color, resistance.values.green.color, resistance.values.blue.color, resistance.values.gold.color ];

	var resistor_canvas = new ResistorCanvas(document.getElementById("resistor"));
	resistor_canvas.draw_resistor(list);

	var resistor2_canvas = new ResistorCanvas(document.getElementById("header-resistor"));
	resistor2_canvas.draw_resistor(list);
}

/**
 * Handles the onchange event for the bands selectors.
 *
 * @param {Number} band Band index.
 * @param {Element} elem Which element fired the event.
 */
function bands_onchange(band, elem) {
	// Check if it's a normal band or the special tolerance one.
	if (band !== "tolerance") {
		// Set band.
		sel_bands.values[band] = elem.options[elem.selectedIndex].value;
	} else {
		// Set tolerance.
		sel_bands.tolerance = elem.options[elem.selectedIndex].value;
	}

	// Calculate and pretty print.
	console.log(resistance.pretty_print(resistance.calc(sel_bands.values), sel_bands.tolerance));
}


/**
 * Helper class to create a resistor using a <canvas>
 *
 * @constructor
 * @param {Element} canvas A canvas element.
 */
function ResistorCanvas(canvas) {
	this.band_width = 0;
	this.curr_stripe = 0;

	this.canvas = canvas;
	this.context = canvas.getContext("2d");
}

/**
 * Draw a single resistor band to the canvas.
 *
 * @param {String} color The band color.
 */
ResistorCanvas.prototype.draw_band = function (color) {
	this.context.fillStyle = color;
	this.context.fillRect(this.band_width * this.curr_stripe++, 0, this.band_width, this.canvas.height);
}

/**
 * Draws the resistor in the canvas.
 *
 * @param {Array} list Bands array.
 */
ResistorCanvas.prototype.draw_resistor = function (list) {
	var resistor_color = "rgb(188, 137, 93)";

	this.band_width = this.canvas.width / ((list.length * 2) + 2);
	this.curr_stripe = 0;

	// Draw the middle stripes.
	for (var i = 0; i < list.length - 1; i++) {
		this.draw_band(resistor_color);
		this.draw_band(list[i]);
	}

	// Draw tolerance band.
	this.draw_band(resistor_color);
	this.draw_band(resistor_color);
	this.draw_band(list[list.length - 1]);
	this.draw_band(resistor_color);
}
<commit_msg>Colors are changing<commit_after>// main.js
// The mains.


var resistor_canvas = null;
var header_canvas = null;

// Selected bands.
var sel_bands = {
	values: [ "black", "black", "black" ],
	tolerance: "gold"
};

/**
 * Just a simple onLoad event handler.
 */
function onload() {
	// Setup some canvas stuff.
	resistor_canvas = new ResistorCanvas(document.getElementById("resistor"));
	header_canvas = new ResistorCanvas(document.getElementById("header-resistor"));

	// Draw stuff in the canvas for the first time.
	update_canvas();
}

/**
 * Handles the onchange event for the bands selectors.
 *
 * @param {Number} band Band index.
 * @param {Element} elem Which element fired the event.
 */
function bands_onchange(band, elem) {
	// Check if it's a normal band or the special tolerance one.
	if (band !== "tolerance") {
		// Set band.
		sel_bands.values[band] = elem.options[elem.selectedIndex].value;
	} else {
		// Set tolerance.
		sel_bands.tolerance = elem.options[elem.selectedIndex].value;
	}

	// Calculate and pretty print.
	console.log(resistance.pretty_print(resistance.calc(sel_bands.values), sel_bands.tolerance));

	// Update the canvas.
	update_canvas();
}

/**
 *	Updates the canvas according to the bands selected.
 */
function update_canvas() {
	var list = sel_bands.values.concat(sel_bands.tolerance);
	for (var i = 0; i < list.length; i++) {
		list[i] = ResistorCanvas.get_color(list[i]);
	}

	resistor_canvas.draw_resistor(list);
	header_canvas.draw_resistor(list);
}


/**
 * Helper class to create a resistor using a <canvas>
 *
 * @constructor
 * @param {Element} canvas A canvas element.
 */
function ResistorCanvas(canvas) {
	this.band_width = 0;
	this.curr_stripe = 0;

	this.canvas = canvas;
	this.context = canvas.getContext("2d");
}

/**
 *	Get the correct color code from the resistance.values JSON.
 *
 *	@param {String} name Color name.
 *	@return {String} Correct color code.
 */
ResistorCanvas.get_color = function (name) {
	return resistance.values[name].color;
}

/**
 * Draw a single resistor band to the canvas.
 *
 * @param {String} color The band color.
 */
ResistorCanvas.prototype.draw_band = function (color) {
	this.context.fillStyle = color;
	this.context.fillRect(this.band_width * this.curr_stripe++, 0, this.band_width, this.canvas.height);
}

/**
 * Draws the resistor in the canvas.
 *
 * @param {Array} list Bands array.
 */
ResistorCanvas.prototype.draw_resistor = function (list) {
	var resistor_color = "rgb(188, 137, 93)";

	this.band_width = this.canvas.width / ((list.length * 2) + 2);
	this.curr_stripe = 0;

	// Draw the middle stripes.
	for (var i = 0; i < list.length - 1; i++) {
		this.draw_band(resistor_color);
		this.draw_band(list[i]);
	}

	// Draw tolerance band.
	this.draw_band(resistor_color);
	this.draw_band(resistor_color);
	this.draw_band(list[list.length - 1]);
	this.draw_band(resistor_color);
}

{'fraction_non_alphanumeric': 0.14118997512913717, 'fraction_numerical': 0.007461258848287737, 'mean_word_length': 4.060987415295257, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3888523', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1732, 'n_words': 442}