Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
idnthvn

Help to make colored Text

Question

Okay, so i am a complete NEWB with lua. Never programmed in my life. 
I have watched the tutorials and none of which help me at all. 

I want to basically copy the idea of ComputerCrafts 

term.setTextColor()

where i can set what i am printing/writing on the screen in color. 

i can do 

local term = require("term")
local component = require("component")
local gpu = compontent.gpu

term.clear()

print(gpu.serForeground(0x2E86C1) .. "Test")

This changes my entire text to a blue. rather then just color the text i want in that line. 

Any help would be great.   Maybe this can be used for beginners to simply be able to make a Rules/Info page on their Server (Which is the idea i am after)

Sorry for the NEWB questions. 

Thank you

Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Well, last i checked ComputerCraft does essentially the same thing without hexadecimal if i recall correctly, but the way i would go about it is on the next line, change it back to white with

gpu.setForeground(0xFFFFFF)

so your finished code would be

local term = require("term")
local component = require("component")
local gpu = component.gpu
term.clear()
print(gpu.setForeground(0x2E86C1) .. "Test")
gpu.setForeground(0xFFFFFF)

 

Link to post
Share on other sites
  • 0

I will try to help you as best as I can.

First of all, this is not CC(Computer craft), so, don't you dare to try and port some code etc. It is kind of similar, and a buch of stuff can be portet, yet still it is different in a way.

In order to have colored things on ya screen you may want to use... I belive tier 2 and up? But just in case, uset T3 screen and T3 gpu.

I will write a bunch of code, hope you will understand, but first a bit of syntax, so you aren't confused. Whenever I will use '---' without quotes, that means that everything after those three dashes is not a part of code and OC won't compile it untill you start new line with enter. And that is all.

So, code...

---Initialization, this is part when you have to define all global variables and APIs to use(and hardware as well).
local term=request('term')---this will allow ya to use some terminal related functions, such as term.clear(), etc. Works with printed text only
local component=request('component')---this will allow you to use components
local gpu=component.gpu---this will allow you to use all GPU related stuff (also a reason why you had to define component)
local forecolor=0xFF0000---this is a hexadecimal numeric value of foreground color in RGB. This is Red as possible.
local backcolor=0x0000FF---this in the other hand is as blue as possible. Google 'hexadecimal colors', you will find all numbers you need.
local w,h=gpu.getResolution()---will get and store your resolution of screen in w-width and h-height

term.clear()---clears text, don't remember if there is any by default
gpu.setDepth(8)---this will set 8-bit color depth (8-bit is max, not sure if by default tho)

---now we can write some stuff
---gpu.set(x:num,y:num,text:string)
gpu.set(1,1,'Hello world!")---this will print 'Hello world' text on screen starting at 0 0 coords. See that I am not using print('text')
---.set is your way to go if you want to play with colors, it is more controllable and relayable than print()
---Text up there will be in default colors, b/w, now let's change it
gpu.setForeground(forecolor)
gpu.setBackground(backcolor)---both self explaning. Just set color of text and it's background.
gpu.set(1,2,'Hello, I am in color now!!!!')---Red text on blue background in line 2.
term.clear()---nothing happens as those typings on screen aren't terminal related, rather GPU specific things.
os.sleep(5)---sleep for 5 sec, cute, isn't it?
gpu.setBackground(0x000000)---black background
gpu.fill(1,1,w,h,' ')---this will fill rectangular area with it's top left corner at 1,1, while being w width and h high. Filled with blank black spaces.
---Screen is cleared after 5 seconds as it will fill area equal to your resolution.
os.exit()---exits program (in case you have loops this is way to go, here it isn't necessary)

So... Sorry for my english btw.

Hope I helped you

Link to post
Share on other sites
  • 0

I think this is what you mean...?

local term = require 'term'
local component = require 'component'
local gpu = component.getPrimary 'gpu'

--          text                   color          palette color? (true or false) (optional)
-- Takes ( 'some string to color', number_color, [isPaletteColor] )
local function cWrite( text, fgc, pIndex )
	local old_fgc, isPalette = gpu.getForeground()
	pIndex = ( type( pIndex ) == 'boolean' ) and pIndex or false
	gpu.setForeground( fgc, pIndex )
	write( text )
	gpu.setForeground( old_fgc, isPalette )
end

-- Usage:
cWrite( 'Hello colorfully', 0x2E86C1 )

-- Or with a palette color..
cWrite( 'Hello colorfully', 1, true )

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.