A JavaScript Library for things I use often, as well as some helper functions

Overview

Elements

A JavaScript Library for things I use often, as well as some helper functions. Full documentation below.

Inspired by Habitat, another library that I help out with!

Installing

You can embed it into the browser like this: (I don't really use Modules, but I can convert this over to one if I ever see the need to)

<script src="Elements.js"></script>

Initialisation

You can just use every function out-of-the-box, like so:

Elements.Math.clamp() // or any other function

But you can also install the entire library into the window object. For instance:

Elements.install(window) // or you can use .install(this)

You can also install specific parts of the library, like so:

Elements.Colour.install(window) // or you can use .install(this)

Sections

Colour

new Colour(type, ...data)
new Elements.Colour(type, ...data)

Creates a new Colour object from the data you provide. It can be of type rgb[a], hsl[a] or even cmy[a]. The types would need to be specified using the type parameter.

You can also pass a previously created Colour object, and it will create a new Colour using the same values, effectively cloning it.

You can also change a value after it has been constructed, like so:

var my_colour = new Colour() // with no arguments, it will initialise to black, or { r: 0, g: 0, b: 0, a: 255 }
my_colour.hsl = [30, 0.5, 0.75] // sets the colour to a light desaturated orange
my_colour.hsl = [60] // changes just the hue to 60* - a yellow colour - keeping the saturation and lightness the same
my_colour.hsl = [undefined, 1] // full saturation

For the last example there, I will probably add .saturation, .lightness or .s, .l values too, but thats not the case yet. Here are all the values you can cange and obtain:

  • .r, .g, .b, .a: Red, Green, Blue and Alpha channels
  • .rgb, .rgba: all the channels, excluding alpha for rgb, as a list
  • .hsl, .hsla: the hue, saturation and lightness values as a list
  • .cmy, .cmya: Cyan, Magenta and Yellow, the colour system for printers! (Might be useful, maybe...)
  • .isElementColour: for determining if an object is a colour defined by this constructor (I'll probably change this to work better in future)

There are other helper functions under the Colour namespace - they include converters between RGB, HSL and CMY colour systems - and are used in the creation of colours. You can use them like so:

Elements.Colour.rgb2hsl(red, green, blue) // returns list [hue, saturation, lightness]
Elements.Colour.hsl2rgb(hue, saturation, lightness) // returns list [red, green, blue]
Elements.Colour.rgb2cmy(red, green, blue) // returns list [cyan, magenta, yellow]
Elements.Colour.cmy2rgb(cyan, magenta, yellow) // returns list [red, green, blue]

I'll probably also do hsl2cmy and cmy2hsl, but I wouldn't know where to start with that yet.

Credit to Kamil Kiełczewski on StackOverflow for the hsl2rgb and rgb2hsl functions

Math

Elements.Math.clamp(number, low, high)
Math.clamp(number, low, high)

Returns the number clamped between the low and high values, for instance:

Math.clamp(5, 0, 10) // 5
Math.clamp(-0.6180339887, 0, 10) // 0
Math.clamp(42, 0, 10) // 10

I plan to add more kinds of 'clamping', things like a modified Sigmoid, wrap, inverted wrap and crop but that's not yet the case.

Comments
  • Add `Meal.error(edible, message)` edible

    Add `Meal.error(edible, message)` edible

    Saw something like this on https://craftinginterpreters.com - it'd be very helpful for making compile errors, rather than runtime errors that you can currently make with Meal.map().

    Would it throw the message as a custom error, or take another errorType argument? 🤔

    enhancement question 
    opened by Magnogen 1
  • Add more ways to update the properties of `Colour` object

    Add more ways to update the properties of `Colour` object

    Test.js

    • Renamed col to teal
    • Added peach - teal but with full red

    Elements.js

    • Added .h, .s, .l, .c, .m and .y
    • Cleaned up the configurable and enumerable property... properties? of Colour object
    opened by Magnogen 1
  • A few major changes

    A few major changes

    • remove install functions
    • move stuff around
      • group getters and setters together and such
    • fix bugs
    • make things shorter
    • rename Meal.error to Meal.need
    opened by Magnogen 0
  • Fix Colour installation and add more Limits

    Fix Colour installation and add more Limits

    • renamed Math to Limit, keeping more inline with its functionality
    • fixed the installation process using the nullish assignment (very pog if you ask me)
    opened by Magnogen 0
  • Colour hue can't update with a 0 saturation or brightness value

    Colour hue can't update with a 0 saturation or brightness value

    Something as simple as

    let col = new Colour();
    col.h = 180;
    col.s = col.l = 0.5;
    

    will still make black as the attempt at updating the hue is done before the saturation or lightness.

    Might need to make h, s and l have their own internal parameters like r, g and b. Then when an RGB is updated, update the hsl, and vice versa. That way a brightness of 0 doesn't mean the hue is automatically 0.

    If that doesn't work, might need to think a bit harder...

    bug 
    opened by Magnogen 0
  • Add CNS colour system

    Add CNS colour system

    "Red", "green", "blue". Super easy.

    Hues, saturations, and lightnesses can be expressed in English - making colour picking work well.

    Maybe also add an accuracy parameter? That way you can get more of a generative colour palette. Could be useful. I like generative stuff.

    Might need to split up the accuracies into hue, sat, and lit too, but idk. Have a think.

    enhancement 
    opened by Magnogen 0
  • Thread handler does waaay too much work

    Thread handler does waaay too much work

    There's an infinite loop in there, so when it reaches a point where it completes a cycle without running a task, it will just do way too much work.

    This can happen when:

    • There aren't any threads yet
    • All the threads are waiting for the next frame

    Basically, fix this.

    bug 
    opened by Magnogen 0
  • `Meal.edible(

    `Meal.edible("...")` ideas

    Something like MotherTode, that Crafting Interpreters syntax and regex, but also completely different.

    Edible written with the edible syntax?

    EDIBLE = (DEFINITION ";")* CHAIN;
    DEFINITION = IDENTIFIER "=" CHAIN;
    CHAIN = ANY (" " ANY)*;
    ANY = LABEL ("|" LABEL)*;
    LABEL = (IDENTIFIER ":")? UNARY;
    UNARY = "!"* PRIMARY ("?" | "*" | "+")*;
    PRIMARY = STRING | REGEX | IDENTIFIER | ("(" CHAIN ")");
    STRING = "\"" ("\\\"" | !"\"") "\"";
    REGEX = "/" ("\\/" | !"/") "/";
    IDENTIFIER = /[A-Za-z_]/ /[A-Za-z_0-9]/*;
    EDIBLE
    
    enhancement 
    opened by Magnogen 0
  • New Features

    New Features

    I'd like to add a few more features, as three isn't that much lol. Any other ideas for this? Hmm

    • [ ] Canvas

      A way to draw a bunch of stuff without having to do arcs for circles translate and rotate for just rotated things, and things like that. P5.js inspired.

    • [ ] Range

      0 ... 5, !0 ... 1, etc, basically a shorter way to do conditions. Based on ColourTode by TodePond

    • [ ] Vector

      add, sub, dot, cross, div, lerp, stuff like that - but for vectors of arbitrary dimensions!

    • [x] State

      Basically FSMs, a way to switch between states and stuff using an object that has objects or whatever.

      I have no idea how to implement this.

    • [x] Thread

      A way to do multiple things in sort-of parallel. Good for generative art that can take a while haha. Pixel manipulation and lots of circles, maybe even simulations and such.

    enhancement 
    opened by Magnogen 0
Owner
Magnogen
Hey! I'm Mag. I like coding, gaming, music - stuff like that. Feel free to peruse my repos if you wish!
Magnogen
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Um bot de suporte feito usando threads para o Discord, 100% customizável, feito em JavaScript e inspirado no Rio Helper do servidor Elixir Lab e na Loritta Helper do serivdor de suporte da Loritta.

Ticket Bot Um bot de suporte feito usando threads para o Discord, 100% customizável, feito em JavaScript e inspirado no Rio Helper do servidor Elixir

ADG 6 Dec 21, 2022
Automated testing for single-page applications (SPAs). Small, portable, and easy to use. Click on things, fill in values, await for things exist, etc.

SPA Check Automated testing for single-page applications (SPAs). Small, portable, and easy to use. Click on things, fill in values, await for things e

Cory Leigh Rahman 5 Dec 23, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Babel plugin and helper functions for interoperation between Node.js native ESM and Babel ESM

babel-plugin-node-cjs-interop and node-cjs-interop: fix the default import interoperability issue in Node.js The problem to solve Consider the followi

Masaki Hara 15 Nov 6, 2022
A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages (https://developers.cloudflare.com/pub-sub/)

pubsub A set of useful helper methods for writing functions to handle Cloudflare Pub/Sub messages. This includes: A isValidBrokerRequest helper for au

Cloudflare 18 Dec 4, 2022
Well-tested utility functions dealing with async iterables

aitertools This library provides a well-tested collection of small utility functions dealing with async iterables. You can think of it as LINQ or aite

Hong Minhee (洪 民憙) 11 Aug 15, 2022
Functions Recipes is a library of examples to help you getting started with Salesforce Functions and get used to their main features.

Functions Recipes Introduction Salesforce Functions lets you use the Salesforce Platform for building event-driven, elastically scalable apps and expe

Trailhead Apps 172 Dec 29, 2022
Temporal-time-utils - This is a library with some reusable functions for Temporal.io TypeScript SDK

temporal-time-utils This is a library with some reusable functions for Temporal.io TypeScript SDK: sleepUntil: sleep to a specific date, instead of by

swyx 15 Oct 18, 2022
In game dev, generative art, and creative coding, sine is a ubiquitous function that is often used as a spring-like oscillator for a given parameter.

In game dev, generative art, and creative coding, sine is a ubiquitous function that is often used as a spring-like oscillator for a given parameter.

Mark Racette 3 Feb 22, 2022
The most often-used OOP design patterns in TypeScript

The most often-used OOP design patterns Generating patterns Factory method Abstract factory Builder Prototype Singleton Structural patterns Adapter Br

Rodion 4 Mar 11, 2022
Quo is a (free) debugging companion app to help you debug dumped variables, the dumped variables will appear in this Quo client instead of the traditional way which is often tedious.

Quo is a debugging companion to help you debug dumped variables, the dumped variables will appear in this Quo client instead of via the traditional way which is often tedious.

Protoqol 33 Dec 25, 2022
Bot that randomly posts 23,463 personalized license plate applications the California DMV received from 2015-2016 to Twitter every so often.

ca-dmv Bot that randomly posts 23,463 personalized license plate applications the California DMV received from 2015-2016 to Twitter every so often. Wa

rj 212 Dec 31, 2022
MidJourney is an AI text-to-image service that generates images based on textual prompts. It is often used to create artistic or imaginative images, and is available on Discord and through a web interface here.

Midjourney MidJourney is an AI text-to-image service that generates images based on textual prompts. It is often used to create artistic or imaginativ

Andrew Tsegaye 8 May 1, 2023
The repository shows the compiler (simulator) of the Little Man Computer, which also contains some programs in the LMC programming language for implementing different functions.

Little Man Computer The repository shows the compiler (simulator) of the Little Man Computer, which also contains some programs in the LMC programming

Cow Cheng 2 Nov 17, 2022
A simple react project that contain a single page application (SPA) And a simple caculator to make some calculation and there is a section you can see some Math quotes. Ⓜ💯

A simple react project that contain a single page application (SPA) And a simple caculator to make some calculation and there is a section you can see some Math quotes. Ⓜ??

Reem janina 7 May 31, 2022
This Repo Contains projects that demonstrate some concepts / algorithms / implemetation in some form of digital visualisation

Hacktoberfest 2022 OPEN FIRST Pull Request - GET STARTED WITH OPENSOURCE AND WIN SOME AWWSOME SWAGS ?? Contributors of Hacktoberfest 2022 This project

null 5 Nov 7, 2022