Suppose that an open source developer wants to use the product name ninjago owed by Lego for the name of an Open Source project. Is it allowed to base the project name on Lego brand? Also can the developer create a unique logo for this open source project that will look kind of similar or has some kind of reference to Lego product? Or is even a name protected?
Trademark law varies somewhat from country to country, and the set of protected trademarks varies a lot from one country to another. In this answer I shall consider primarily US law, although I will try to make the answer as wide ranging as I can.
What trademark law protects is the identification of a source of goods or services, and the associated reputation an good will. By using a trademark that is identical to or confusingly similar to an exiting mark, one may be relying to some extent on the reputation of the existing brand. Unless done with permission, this is unfair, and is generally not allowed.
However, the protection of a trademark is normally limited to a product or service of the same kind as the existing mark. So suppose one firm registered "GreenCall" as the name of a smartphone service. Another firm wanting to use "Greencall" as the name of a wine would probably not be infringing on the first firm's trademark, and could not be successfully sued. (They might well be sued, however, and need to spend significant money defending their mark.)
In some countries, only registered trademarks get any protection. In others, such as the US, use of a mark without registration will provide some protection, but not as much as registration does.
In the US at least,a well-known or "famous" mark gets addition protection, even outside the category where it has been registered and used. What counts as "famous" is ultimately a decision fo the US Patent and Trademark office (USPTO) to make.
A toy line set in a fantasy world would seem like a rather different category than an open-source project, unless the project was a game with similar elements. The farther and more obviously different the project might be from the toy line, the less likely the use of the same name would be to be held a trademark infringement.
Since the Lego mark is used not only on toys, but on a successful TV series, a movie, a video game, and a theme park, it might be held to be a "well-known" mark, and be protected even in categories it is not used in.
Actually the project being an open-source one would make no significant difference here. Open source project and commercial products follow the same trademark rules.
However, even if the category is different enough that the use of the same name is ultimately held not to be trademark infringement, it is not unlikely that the trademark owner would take legal action, and the developer or development group would need to spend money on legal fees at n early stage of the project. It might be wiser to choose a more clearly distinctive name. It might also be wise to consult a lawyer with trademark expertise.
As to the logo, if the logo in some way "looks similar" to an existing logo, it might be held to be a derivative work, and thus a copyright infringement. This is a highly fact-dependent judgement call.