JavaScript as text
If your question is whether you can create a text that contains a JS code that will be inserted along with the HTML text you are generating with PHP. Yes, you can.
What PHP does is just this, generate a "language" text that will be sent to the HTTP server it will send to the browser or who has requested it. It may be that it is not an HTML page you are doing. Understand PHP code as a great word processor.
Of course it can do much more than this, but in the way it is used the final result will always be text that can contain anything you want, it can have HTML, JS, CSS, JSON, plain text without formatting, or other format. Actually it does not even need to be text, this is the kind of data generated for most common output. If the text has something wrong inside it, it is not PHP problem.
It may seem confusing because it is the code of a language within the code of another language. These codes are unrelated. They do not "talk", even because the JS code is not executed by PHP. For him, this is just some text. Only you know that's JS. When programmed, think of PHP code independently of JS. The problem of one is not another's problem.
The echo
is just for this. Everything you "print" in PHP will be sent to the HTTP server. At least this is the most common way to use PHP (you can use it in other ways, but most programmers do not know how to do this and do not even know it's possible). Of course, where to put this echo
in the code is important to achieve the expected result.
Have you tried to see in the browser the result of a page generated with PHP? Make experiments trying to send in different ways and whenever you have specific questions, post here giving as much details as possible of what you are doing and what the problem is.
JavaScript running
If you want to know if you can insert this JS code into your own PHP code and run it yourself, then the answer is no, you can not do this. They are distinct languages. There may even be some way to call a mechanism that executes the JS externally, but it's something that nobody does and it's really weird. I see no reason to do this.
Specific problem
Note that ideally you do not have JS code inserted inside an HTML page. It would be better to have all JS code in a separate file, and preferably this file would be static, ie fixed and not generated on the fly.
Generating JS code dynamically via PHP usually makes little sense most of the time. Inserting it into JS also is not usually necessary, although it can be used without any problems.
After editing the question I see that the code is doing what you want, or not? Did he put it to perform? Did he appear in the browser? This is it. Of course this code assumes that other HTML parts or even JS parts on the same page or additional file.
But I have my doubts if you need to generate all this JS code in PHP. I think you can leave the JS separate and set it to handle the color per parameter and only the color to be inserted into the call of this code. You need to see a larger context. But reinforcing what I said before, usually does not have to generate JS code by PHP.
If JS is not generated ok and does not work, the problem is another one described in the question, so you should open another question with your specific problem. Post the rest of the page code in this new question. I do not think PHP is necessary, not even because it was actually placed.