www.webdeveloper.com
Results 1 to 11 of 11

Thread: Convert Microsoft Access Color Code to R,G,B

  1. #1
    Join Date
    Feb 2009
    Posts
    26

    Convert Microsoft Access Color Code to R,G,B

    Hi,

    I'm a beginner and need, for an eLearning lesson, to convert the output from a color picker DLL which exports using a Microsoft Access (Office) color code (for example: 16711680) to a R, G, B format, for example: 0,255,0.

    The eLearning authoring tool uses an older form of JavaScript called EMCA-262.

    As a beginner, I'm not sure how to write a JS script to convert.

    Any help appreciated.

    saratogacoach

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    943
    This is actually much simpler than you might expect. Access essentially just uses the decimal version of the color codes, rather than the hexadecimal version. The only other important thing to note is that instead of R, G and B being the sequence, Access displays it the order of B, G and R.

    So, now for the actual code part.
    Code:
    function _AccessToHex($a) {
    	return $a.toString(16).split("").reverse().join("");
    }
    function _HexToAccess($a) {
    	return parseInt($a.split("").reverse().join(""), 16);
    }
    These two functions can convert between the two formats respectively. One important thing to note is that when using _AccessToHex, you should be passing a number value (not a string). So two examples would look like:
    Code:
    _AccessToHex(16711680); // Would return 0000ff as a string
    _HexToAccess("0000FF"); // Would return 16711680 as an integer/number
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Feb 2009
    Posts
    26
    Hi,

    Thank you for this. I am trying to get it to work in the eLearning software tool (Opus Pro), but so far, while it doesn't show any errors in Opus's script editor, it displays a Debug.trace window error for the functions as unknown. I suspect that while they are perfectly good JS, Opus's older basic JS (a subset of ECMA-262) doesn't have an "$" operator, has "split" and "reverse" for arrays, and doesn't have "join."

    I wish there were a way to write this so that Opus script can run it. So, I'll keep trying to use the underlying concepts to re-write it into Opus script, if possible, find a work-around.

    Thank you again.

    Kind Regards,
    Last edited by saratogacoach; 05-01-2014 at 07:38 PM.

  4. #4
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    if the value from Access is an integer, you could do bitwise on it
    Code:
    function int2rgb(n)
    {
        n = parseInt(n, 10);
    
        if (isNaN(n))
            throw TypeError();
        var r = (n >> 0) & 0xff, g = (n >> 8) & 0xff, b = (n >> 16) & 0xff;
        return r.toString(16) + g.toString(16) + b.toString(16);
    }
    function rgb2int(s)
    {
        if (!/^[0-9a-f]+$/i.test(s) && (s.length != 6 || s.length != 3))
            throw TypeError();
        if (s.length == 3) { // shorthand "FFF"
            s = s.charAt(0) + s.charAt(0)
                + s.charAt(1) + s.charAt(1)
                + s.charAt(2) + s.charAt(2);
        }
        var r = s.substr(0, 2), g = s.substr(2, 2), b = s.substr(4, 2);
        return (parseInt(r, 16) << 16) | (parseInt(g, 16) << 8) | (parseInt(b, 16) << 0);
    }

  5. #5
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    943
    The dollar sign ($) isn't an operator. It's part of a variable name and should be valid, even in an older, more basic version of javascript.

    So the real culprit here then seems to be the lack of .join(). The only reason it's used in my code is because the MS Access string needs to be reversed, but of course .reverse() is not a supported method of strings. Thus it is converted to an array (.split()), reversed (.reverse()) and then merged back together (.join()) into a string.

    The reverse method can be substituted (for a less efficient method). Using basic string concatenation and loops you can reserve the string. You would just build a function like so:
    Code:
    function _Reverse($a) {
    	var $b = '';
    	for(var $i = $a.length-1; $i >= 0; $i--) {
    		$b += $a[$i];
    	}
    	return $b;
    }
    And so this could be used to reverse the strings before and after conversion (respectively):
    Code:
    function _Reverse($a) {
    	var $b = '';
    	for(var $i = $a.length-1; $i >= 0; $i--) {
    		$b += $a[$i];
    	}
    	return $b;
    }
    
    function _AccessToHex($a) {
    	return _Reverse($a.toString(16));
    }
    function _HexToAccess($a) {
    	return parseInt(_Reverse($a), 16);
    }
    Calling the function to convert between the two formats would remain the same.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  6. #6
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    Quote Originally Posted by saratogacoach View Post
    The eLearning authoring tool uses an older form of JavaScript called EMCA-262.
    I skimmed through the site real quick to see what edition of the ECMA-262 it used couldn't find any info on it.
    From the site:
    From simple games and elearning resources to complex simulations, commercial programs and database applications with or without programming.

    Publish and distribute to Android, iPad, Mac, Windows, HTML5, Flash, SCORM and Moodle as well as .EXE, CD-Rom and DVD-video for pleasure, promotion or profit.
    The first sentence is pretty comical.

    Recommendation: /uninstall, if you put money towards this, get refund and/or put a stop on the credit/debit transaction.
    There are a lot of open-source and/or freeware solutions from wysiwyg editors to bare-bone text editors.
    IMHO, the less an editor has, the better it is. For instance, I use Dreamweaver mostly, 95% of the features in it, I don't use. One really nice open-source editor (oddly enough from Adobe) that I have tried is Brackets. Brackets does not have a lot of feature out of the box. The features it does have are typically the ones you'll utilize the most (i.e. code hints, syntax coloring/highlighting, error checking, code formatting, etc.).

  7. #7
    Join Date
    Feb 2009
    Posts
    26
    Hi,

    Much thanks for these suggestions. I plan to continue to try to get this to work using Opus, which I've been using for several years.

    Hopefully, with these scripting suggestions, I will be able to "cobble together" a script that, although maybe less efficient than using a fuller set of JS functions, will never-the-less do the job: convert the Access color code to R,G,B.

    Again, thank you for your help.

    Best Wishes,

  8. #8
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    943
    First, a +1 to ShrineDesigns for Brackets. I think I'll be giving that a try.

    Now to saratogacoach, while you should be able to use any of the later scripts posted in this topic in your Opus software, I do have to ask, have you actually tried saving the page and opening it in a browser? Regardless of the software you use to build your pages, they are ultimately rendered in a web browser. So even if Opus doesn't like newer javascript methods thrown into a script, that won't stop it from working in any relatively modern web browser. Thus I can't see any reason why the original code won't work once the page is actually loaded into a browser (and not being displayed via Opus).
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  9. #9
    Join Date
    Feb 2009
    Posts
    26
    Hi Sup3rkirby,

    Thank you for your follow up. Opus in this project exports an EXE for Windows deployment. So, not deployed to web browser.

    My scripting skills are very limited: most Opus actions (for example, show/hide an object) do not require scripting, although as an alternative, they can be scripted, using the Opus script language. Some ECMA-262/JS has made its way into Opus script, while other items are not available.

    When I try to adapt the posted scripts and put them into the Opus scrip object/editor, while they do not throw editor errors, when previewed or compiled they unfortunately they have errors, I suspect based on one or another item not being available in Opus script (or my limited scripting skills, for example around declaring and populating arrays, resulting in my mistakes). I've posted on the Opus forum to see if a more skilled Opus scripting person may respond with help "translating" these posted scripts into an Opus script that will work in Opus.

    There is even an Opus script to convert Access code to Hex, which an Opus forum member wrote a while ago, but I can't even pop it into Opus without similar errors. (I can't find "unshift" as an item in the Opus script reference guide. Maybe another way to do "unshift"? I also think that the arrays need to be declared before the function can be run.)

    Code:
    function decColor2hex(color) {
       colArr = color.toString(16).toUpperCase().split('');
       numChars = colArr.length
       for (a = 0; a < (6 - numChars); a++) {
          colArr.unshift("0")
          }
       var a = colArr[0]
       var b = colArr[1]
       var c = colArr[2]
       var d = colArr[3]
       var e = colArr[4]
       var f = colArr[5]
       return ("#"+e+f+c+d+a+b);
    }
    I'll keep trying.

    Again, thank you for these suggestions. I'm learning more about this and will keep trying to get it to work in Opus.

    Kind Regards,
    Last edited by saratogacoach; 05-02-2014 at 01:34 PM.

  10. #10
    Join Date
    Feb 2009
    Posts
    26
    OK, as a start, this works:

    Code:
    var colArr = new Array()
    var color = 16711680
    colArr = color.toString(16).toUpperCase().split('');
    numChars = colArr.length
       for (a = 0; a < (6 - numChars); a++) {
          colArr.unshift("0")
          }
       var a = colArr[0]
       var b = colArr[1]
       var c = colArr[2]
       var d = colArr[3]
       var e = colArr[4]
       var f = colArr[5]
       var aaa = ("#"+e+f+c+d+a+b);
       Debug.trace(aaa)
    Debug.trace displays #0000FF

    I'll try to adapt it further.

    Again, much thanks!

  11. #11
    Join Date
    Feb 2009
    Posts
    26
    Update:

    Code:
    var colArr = new Array()
    var color = 65280
    colArr = color.toString(16).toUpperCase().split('');
    numChars = colArr.length
       for (a = 0; a < (6 - numChars); a++) {
          colArr.unshift("0")
          }
       var a = colArr[0]
       var b = colArr[1]
       var c = colArr[2]
       var d = colArr[3]
       var e = colArr[4]
       var f = colArr[5]
       var aaa = ("#"+e+f+c+d+a+b);
       Debug.trace(aaa)
    If I use a Access code with fewer numbers like 65280 (green) in the above code (instead of 16711680 which is blue), I get an error message.

    So, I suspect that a different script that accounts for more and less numbers in the Access code would be needed?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles