Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS.setBackground cannot cope with three digit colour #1052

Closed
ThePix opened this issue Sep 18, 2018 · 5 comments · Fixed by #1304
Closed

JS.setBackground cannot cope with three digit colour #1052

ThePix opened this issue Sep 18, 2018 · 5 comments · Fixed by #1304

Comments

@ThePix
Copy link
Contributor

ThePix commented Sep 18, 2018

HTML/CSS should be able to cope with #rgb as well as #rrggbb. However, JS.setBackground attempts to slit the string into the three components, and is not built to take the former, resulting in a JS error.

NB: Different versions in playercore.js and paper.js, but neither can handle this.

@KVonGit
Copy link
Collaborator

KVonGit commented Aug 24, 2024

This seems to fix it. Be sure to test it out, though. (I tend to overlook possibilities when testing things like this.) 1

THIS CODE DOESN'T WORK IN QUEST DESKTOP)

(Found the code here: https://www.geeksforgeeks.org/how-to-convert-3-digit-color-code-to-6-digit-color-code-using-javascript/)

function setBackground(col) {
    /* If '#rgb', convert to '#rrggbb'*/
    if (col.startsWith("#") && col.length == 4) {
        /* var colBak = col */
        col = col.split("").map((item)=>{ if(item == "#"){return item} return item + item; }).join("")
        /* console.log ("DEBUGGING: setBackground() changed \"" + colBak + "\" to \"" + col "\" +.") */
    }
    colNameToHex = colourNameToHex(col);
    if (colNameToHex) col = colNameToHex;
    rgbCol = hexToRgb(col);
    var cssBackground = "rgba(" + rgbCol.r + "," + rgbCol.g + "," + rgbCol.b + "," + _backgroundOpacity + ")";
    $("#gameBorder").css("background-color", cssBackground);

    $("#gamePanel").css("background-color", col);
    $("#gridPanel").css("background-color", col);
}

Footnotes

  1. Yep. I forgot the desktop player has a very old version of JS that doesn't recognize =>. See my next post for the good code.

@KVonGit
Copy link
Collaborator

KVonGit commented Aug 25, 2024

I tested the above code in the dev tools console in Firefox.

I forgot the => Javascript stuff doesn't work in the desktop player.

Here is actual good, tested code (EDITED TO REMOVE DEBUGGING MESSAGE):

String.prototype.startsWith = function(s){ return this.charAt(0) === s; }

function setBackground(col) {
  /* If '#rgb', convert to '#rrggbb'*/
  if (col.startsWith("#") && col.length == 4) {
    var colBak = "" + col + "";
    colArr = col.split("")
    var newCol = "";
    for (var i=0; i<colArr.length;i++){
      if (colArr[i] == "#"){
        newCol = newCol + colArr[i];
      } else {
        newCol = newCol + colArr[i] + colArr[i];
      }
    }
    col = newCol || col;
    /* console.log ("DEBUGGING: setBackground() changed \"" + colBak + "\" to \"" + col + "\".") */
  }
  colNameToHex = colourNameToHex(col);
  if (colNameToHex) col = colNameToHex;
  rgbCol = hexToRgb(col);
  var cssBackground = "rgba(" + rgbCol.r + "," + rgbCol.g + "," + rgbCol.b + "," + _backgroundOpacity + ")";
  $("#gameBorder").css("background-color", cssBackground);
  $("#gamePanel").css("background-color", col);
  $("#gridPanel").css("background-color", col);
}

@ThePix
Copy link
Contributor Author

ThePix commented Aug 26, 2024 via email

@KVonGit
Copy link
Collaborator

KVonGit commented Aug 26, 2024

have you considered using regular expressions for this?

I don't know why, but I never even think about using regular expressions. I have nothing against them.

I'll plug that in and see what happens.

Do you know if paper.js needs a function tweaked too, or is this function the only thing that calls the function in paper.js?

@KVonGit
Copy link
Collaborator

KVonGit commented Aug 26, 2024

It seems to work fine with the regex.

Here is a ZIP with a small test game file and the JS file.

JS setBackground Test.zip


I opened the HTML tools and tested it quite a few times from the console. Seems to work fine.


String.prototype.startsWith = function(s){ return this.charAt(0) === s; }

function setBackground(col) {
  /* If '#rgb', convert to '#rrggbb'*/
  if (col.startsWith("#") && col.length == 4) {
    var colBak = "" + col + "";
    newCol = col.replace(/#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/, '#$1$1$2$2$3$3');
    col = newCol || col;
    console.log ("DEBUGGING:(playercore.js) setBackground() changed \"" + colBak + "\" to \"" + col + "\".")
  }
  colNameToHex = colourNameToHex(col);
  if (colNameToHex) col = colNameToHex;
  rgbCol = hexToRgb(col);
  var cssBackground = "rgba(" + rgbCol.r + "," + rgbCol.g + "," + rgbCol.b + "," + _backgroundOpacity + ")";
  $("#gameBorder").css("background-color", cssBackground);
  $("#gamePanel").css("background-color", col);
  $("#gridPanel").css("background-color", col);
}

TextMisadventures added a commit to TextMisadventures/quest that referenced this issue Nov 25, 2024
- transcript
  - added missing function to player.js
  - changed desktop player file saving format from HTML to TXT
  - added localStorage functionality for the web player
  - removed in-game transcript viewing
  - automatically uses game name for transcript name when command bar is hidden

Closes textadventures#1054

---
- log
  - restored log window in the desktop player
  - removed ability to view the log in-game

Closes textadventures#1025

---
- AllRooms()
  - fixed by @Pertex

Closes textadventures#1202

---
- setBackground()
  - fixed by @ThePix

Closes textadventures#1052

---
- enter_verb
  - fix stolen from mrangel forum post

Closes textadventures#1121

---
- save command
  - now uses `saveGame()` when online
  - scrolls when printing saveGameResponse messages

Closes textadventures#1200

---
- DictionaryRemove
  - fixed typo

---
- update transcript.md doc

---

Tidy up language files

- remove code that "includes" EditorEnglish.aslx when English.aslx is already included

---
Addition submitted by @Pertex

Added option to use custom image in the location bar, complete with editor controls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment