Home /Modules/alphabet/Color.sleela

Color.sleela

lib/alphabet/Color.sleela · 146 line(s) · sleela

<< lib/alphabet/Color.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines Color, the smallest unit of delight in the alphabet
     package โ€” one RGB color, held as three channels of 0..255, with the hex
     string, blending, and luminance every user of Sleela quietly relies on.
   
   Color is a NATURAL / LOADABLE MODULE sharing the common module contract
   (load / loaded / moduleName / describe). It is thematically important to ALL
   users of Sleela: every Theme is, at heart, a handful of Colors that agree to
   get along. It holds channel state only; a Palette groups Colors and a Theme
   assigns them roles.
   
   Family: alphabet. >>
#sleela 2.5
material;
< Unified SLeeLa SDK front-end object. >
class Color {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 10302;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-07";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  static final String MODULE = "Color";

  < 1 once the module is loaded >
  int loadedFlag;
  < red   channel, 0..255 >
  int r;
  < green channel, 0..255 >
  int g;
  < blue  channel, 0..255 >
  int b;

  < --- loadable-module contract ------------------------------------------ >
  void load() { loadedFlag = 1; r = 0; g = 0; b = 0; }
  boolean loaded() { return loadedFlag == 1; }
  String moduleName() { return MODULE; }

  < Set the three channels, each clamped to the honest 0..255 range. >
  void set(int red, int green, int blue) {
    r = channel(red);
    g = channel(green);
    b = channel(blue);
  }

  int channel(int v) {
    if (v < 0) { return 0; }
    if (v > 255) { return 255; }
    return v;
  }

  int red() { return r; }
  int green() { return g; }
  int blue() { return b; }

  < Perceived luminance, 0..255 (ITU-R BT.601 weights, integer math). >
  int luminance() { return (r * 299 + g * 587 + b * 114) / 1000; }

  < Is this a "dark" color? Useful for deciding what text to put on top. >
  boolean isDark() { return luminance() < 128; }

  < Blend halfway toward another color โ€” the diplomacy of design. >
  Color blendHalf(Color other) {
    Color out = new Color();
    out.load();
    if (other == null) { out.set(r, g, b); return out; }
    out.set((r + other.red()) / 2, (g + other.green()) / 2, (b + other.blue()) / 2);
    return out;
  }

  < Two colors are equal when all three channels agree. >
  boolean equalsColor(Color other) {
    if (other == null) { return false; }
    return r == other.red() && g == other.green() && b == other.blue();
  }

  < Lowercase hex like "#1e90ff". Built with substring math โ€” no char type. >
  String hex() { return "#" + hex2(r) + hex2(g) + hex2(b); }

  < One byte (0..255) to a two-digit lowercase hex string. >
  String hex2(int v) { return hexDigit(v / 16) + hexDigit(v - (v / 16) * 16); }

  String hexDigit(int d) {
    if (d == 0)  { return "0"; }
    if (d == 1)  { return "1"; }
    if (d == 2)  { return "2"; }
    if (d == 3)  { return "3"; }
    if (d == 4)  { return "4"; }
    if (d == 5)  { return "5"; }
    if (d == 6)  { return "6"; }
    if (d == 7)  { return "7"; }
    if (d == 8)  { return "8"; }
    if (d == 9)  { return "9"; }
    if (d == 10) { return "a"; }
    if (d == 11) { return "b"; }
    if (d == 12) { return "c"; }
    if (d == 13) { return "d"; }
    if (d == 14) { return "e"; }
    return "f";
  }

  String yn(boolean v) { if (v) { return "yes"; } return "no"; }

  String describe() {
    return MODULE + " " + hex() + " rgb=(" + r + "," + g + "," + b + ")"
         + " luminance=" + luminance() + " dark=" + yn(isDark());
  }
}